0

MVC3 では、登録フォームを 1 つ作成しました。1 つのモデル、コントローラー、ビューを作成しました。これは私のコードです:

   [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>

}

しかし、アクション メソッド パラメーターのobjsオブジェクトpublic actionresult Insert(models.Empoyees objs)は null 値を示しています。イミーンEname=NullDepartment=0Emailid=NullおよびAddress=null

4

2 に答える 2

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.

于 2012-10-26T17:47:21.267 に答える
1

これを交換

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

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

これで問題が解決します。

于 2012-10-26T17:40:13.257 に答える