title: How to save to database Client IP Address of Visitors Machine in ASP.Net MVC?
body: <p>am trying to get IP Address of client Machine <code>Request.ServerVariables["HTTP_X_FORWARDED_FOR"];</code> it's working fine.</p>
  
  <p>Get IP On page load</p>
  
  <pre><code>public ActionResult AddCompany()
          {
              get_IP();
              return View();
          }
  </code></pre>
  
  <p>get_IP code</p>
  
  <pre><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;
          }
  </code></pre>
  
  <p>But when user register the form get IP address value also save in the data base..</p>
  
  <p>Cs.html File</p>
  
  <pre><code>&lt;div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"&gt;
      &lt;h1 class="page-header"&gt;Add Company&lt;/h1&gt;
  
      @using (Html.BeginForm())
      {
          @Html.AntiForgeryToken()
  
          &lt;div class="form-horizontal"&gt;
              @Html.ValidationSummary(true)
              @Html.HiddenFor(model =&gt; model.IPAddress)
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.CompanyName, new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.CompanyName)
                      @Html.ValidationMessageFor(model =&gt; model.CompanyName)
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.ShortName, new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.ShortName)
                      @Html.ValidationMessageFor(model =&gt; model.ShortName)
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Email, new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.Email)
                      @Html.ValidationMessageFor(model =&gt; model.Email)
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Country, new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @*@Html.EditorFor(model =&gt; model.Country)*@
                      @Html.DropDownList("Country", null, "---Select Country----")
                      @Html.ValidationMessageFor(model =&gt; model.Country)
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;b&gt;State: &lt;/b&gt;
                  &lt;select id="state"&gt;&lt;/select&gt;&lt;br /&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;b&gt;City: &lt;/b&gt;
                  &lt;select id="city"&gt;&lt;/select&gt;&lt;br /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.MobileNo, new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.MobileNo)
                      @Html.ValidationMessageFor(model =&gt; model.MobileNo)
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;div class="col-md-offset-2 col-md-10"&gt;
                      &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      }
  
      &lt;div&gt;
          @Html.ActionLink("Back to List", "Index")
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I create one <code>@Html.HiddenFor(model =&gt; model.IPAddress)</code> hidden filed value but it's not working ..</p>
  
  <p>On page load IPAddress Value is showing .after i click to save button the <code>Ip</code> value is not saved to the database..</p>
  
  <p>AddCompany </p>
  
  <pre><code>[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);
  
          }
  </code></pre>
  
  <p>Any idea How to save  client IP Adrress from the database..</p>
  
  <p>Class File</p>
  
  <pre><code>[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; }
      }
  </code></pre>