0

In MVC3, i created one register form. I created one model,,controller and view. This is my code:

   [HttpGet]
    public ActionResult Insert()
    {
        Models.Employees objEmpl = new Models.Employees();
        return View(objEmpl);
    }

    [HttpPost]
    [AcceptVerbs("Post")]
    public ActionResult Insert(Models.Employees objs)
    {
        var v = new Models.test().InsertDl(objs);
        return View();
    }
  The above is my controller

   @model MvcVideo.Models.Employees
 @{
   ViewBag.Title = "Insert";
    Layout = "~/Views/Shared/VideoLayout.cshtml";
  }
<h2>Insert</h2>
 @using(Html.BeginForm("Insert","Home",FormMethod.Post ))
 {
  <table>
 <tr>
   <td>
      Employee Name
   </td>
   <td>
     @Html.TextBox("Ename",@Model.Enames)
   </td>
 </tr>
 <tr>
   <td>
     Department Id
   </td>
   <td>
     @Html.TextBox("Departmentid",@Model.DepartId )
   </td>
 </tr>
 <tr>
   <td>
      Email Id
   </td>
   <td>
      @Html.TextBox("Emailid",@Model.EmailIds) 
   </td>
 </tr>
  <tr>
    <td>
      Address
    </td>
    <td>
       @Html.TextBox("Address",@Model.Adress)
    </td>
  </tr>
  <tr>
    <td colspan="2" style="text-align:center;" >          
    <button  title ="Ok"   value="OK"></button>   
    </td>
  </tr>
</table>

}

But the objs object at public actionresult Insert(models.Empoyees objs) action method parameter is showing null values. Imean Ename=Null, Department=0, Emailid=Null and Address=null

David Cain
  • 16,484
  • 14
  • 65
  • 75
Ssasidhar
  • 475
  • 4
  • 12
  • 25

2 Answers2

2

It isn't working because the names you've provided in your Html helpers don't match up with the property names on your model, so the default model binder can't resolve them when the values get posted back.

Using Html.TextBoxFor instead of Html.TextBox will provide you with strong typing against your model, and is the safer approach.

ischell
  • 124
  • 4
1

Replace this

@Html.TextBox("Ename",@Model.Enames)

With

@Html.TextBoxFor(model => model.Enames)

This will fix your issue.

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32