簡體   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