2017-09-13 2 views
0

Ich versuche, die IP-Adresse des Clients Maschine Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; bekommen es funktioniert gut.So speichern Sie in Client-IP-Adresse des Besucher-Computers in ASP.Net MVC?

Get IP auf Seite Last

public ActionResult AddCompany() 
     { 
      get_IP(); 
      return View(); 
     } 

get_IP Code

public void get_IP() 
     { 
      string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
      if (string.IsNullOrEmpty(ipAddress)) 
      { 
       ipAddress = Request.ServerVariables["REMOTE_ADDR"]; 
      } 
      ViewBag.IPAddress = ipAddress; 
     } 

Aber wenn Benutzer registrieren die Form erhält IP auch Adresswert in den Basisdaten speichern ..

Cs.html Datei

<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> 
    <h1 class="page-header">Add Company</h1> 

    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 

     <div class="form-horizontal"> 
      @Html.ValidationSummary(true) 
      @Html.HiddenFor(model => model.IPAddress) 
      <div class="form-group"> 
       @Html.LabelFor(model => model.CompanyName, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.CompanyName) 
        @Html.ValidationMessageFor(model => model.CompanyName) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.ShortName, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.ShortName) 
        @Html.ValidationMessageFor(model => model.ShortName) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Email) 
        @Html.ValidationMessageFor(model => model.Email) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @*@Html.EditorFor(model => model.Country)*@ 
        @Html.DropDownList("Country", null, "---Select Country----") 
        @Html.ValidationMessageFor(model => model.Country) 
       </div> 
      </div> 

      <div class="form-group"> 
       <b>State: </b> 
       <select id="state"></select><br /> 
      </div> 
      <div> 
       <b>City: </b> 
       <select id="city"></select><br /> 
      </div> 
      <div class="form-group"> 
       @Html.LabelFor(model => model.MobileNo, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.MobileNo) 
        @Html.ValidationMessageFor(model => model.MobileNo) 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Create" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 
</div> 

Ich erstelle ein abgelegt Wert versteckt, aber es funktioniert nicht ..

Auf Seite Last IPAddress Wert zeigt .after i klicken zum Speichern der Ip Wert wird nicht in der Datenbank gespeichert ..

AddCompany

[HttpPost] 
     public ActionResult AddCompany(Company cmp) 
     { 
      using (DataContext entities = new DataContext()) 
      { 
       entities.Company.Add(cmp); 
       entities.SaveChanges(); 
       int id = cmp.CompanyID; 
       Session.Clear(); 
       Response.Redirect("Index"); 
      } 
      return View(cmp); 

     } 

Jede Idee, wie aus der Datenbank-Client-IP adrress speichern ..

Klasse Datei

[Table("MySecondMVCDemo")] 
    public class Company 
    { 
     [Key] 
     public int CompanyID { get; set; } 
     [Required(ErrorMessage = "Please Enter Company Name")] 
     [Display(Name = "CompanyName")] 
     public string CompanyName { get; set; } 
     [Required(ErrorMessage = "Please Enter Short Name")] 
     [Display(Name = "ShortName")] 
     public string ShortName { get; set; } 
     [Required(ErrorMessage = "Please Enter Email Address")] 
     [Display(Name = "Email")] 
     public string Email { get; set; } 
     [Required(ErrorMessage = "Please Enter Email Address")] 
     [Display(Name = "Country")] 
     public string Country { get; set; } 
     [Required(ErrorMessage = "Please Enter Mobile No")] 
     [Display(Name = "MobileNo")] 
     public string MobileNo { get; set; } 
     public Int32? IPAddress { get; set; } 
    } 

Antwort

1

Ich weiß nicht, über HiddenFor zu bekommen, weil ich es nicht vor dem Gebrauch haben . Wie ich versteckte Felder war zuvor wie folgt aus:

@Html.TextBoxFor(model => model.IPAddress, new { @class = "sr-only", @type = "hidden" }) 

Sie können die Bootstrap-Klasse hinzufügen sr-only um sicherzustellen, dass es versteckt ist. Stellen Sie sicher, dass Sie in Ihrem Browser-Inspektor überprüfen, ob das ausgeblendete Feld einen Wert aufweist.

Wenn gebucht und spart noch nicht, dann versuchen, die IpAddress Identität zu dem Formularfeld Wert zuweisen, bevor es wie folgt zu speichern:

cmp.IPAddress = Request.Form["IPAddress"]; 
Verwandte Themen