繁体   English   中英

HTTP POST,在MVC中获取?

[英]HTTP POST, GET in MVC?

在MVC3中,我创建了一个注册表。 我创建了一个模型,控制器和视图。 这是我的代码:

   [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)动作方法参数表示空值。 Imean Ename Ename=NullDepartment=0Emailid=NullAddress=null

它不起作用,因为您在HTML帮助器中提供的名称与模型上的属性名称不匹配,因此默认模型联编程序无法在将值回传后解析它们。

使用Html.TextBoxFor代替Html.TextBox将为您的模型提供强大的输入,这是更安全的方法。

取代这个

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

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

这样可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM