2014-06-28 100 views
0

我正在學習ASP.NET MVC。我正在嘗試捕獲表單上的模型數據,但模型顯示爲空。模型未在表單發佈過程中填充ASP.NET MVC

這裏是我的模型

public class SampleModel 
{   
    public int ID { get; set; }  
    public string Name { get; set; } 
    public string CRUDOperation { get; set; } 
} 

這是我的看法

@model IEnumerable<SampleModel> 

@using (Html.BeginForm("SubmitUpdateGridRow", "GridView", FormMethod.Post, new {value =  "form" })) 
{ 
@Html.AntiForgeryToken() 
<table class="table table-bordered"> 
    <tr> 
     <th> 
      @Html.Label("CRUD Actions") 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
    </tr> 
    @foreach (SampleModel Row in Model) 
    { 
     <tr> 
      @if (Row.CRUDOperation == "Select") 
      { 
       <td> 
        @Html.ActionLink("Update", "UpdateGridRow", "GridView", Row, new { @title = "U: Update Operation of CRUD" }) | 
        @Html.ActionLink("Edit", "EditGridRow", "GridView", new { id = Row.BGID }, new { @title = "U: Update Operation of CRUD" }) | 
        @Html.ActionLink("Delete", "DeleteGridRow", "GridView", new { id = Row.BGID }, new { @title = "D: Delete Operation of CRUD" }) | 
        @Html.ActionLink("Details", "DetailsGridRow", "GridView", new { id = Row.BGID }, new { @title = "Form level view for details" }) 
       </td> 
       <td>      
        @Row.ID 
       </td> 
       <td> 
        @Row.Name 
       </td> 
      } 
      else if (Row.CRUDOperation == "Edit") 
      {           
       <td> 
        @Html.HiddenFor(ID => Row.ID) 
        @Html.HiddenFor(CRUDOperation => Row.CRUDOperation) 
        @Row.BGID 
       </td> 
       <td> 
        @Html.EditorFor(Name => Row.Name) 
       </td> 
       <td>       
        <input type="submit" value="Update" id="UpdateSubmit" />       
        @Html.ActionLink("Edit", "EditGridRow", "GridView", new { id = Row.ID }, new { @title = "U: Update Operation of CRUD" }) | 
        @Html.ActionLink("Reset", "ResetGridRow", "GridView", new { id = Row.ID }, new { @title = "R: Reset Operation of CRUD" })       
       </td> 
      } 
     </tr> 
     @*<tr> 
      @if (Row.CRUDOperation == "Edit") 
      { 
       EditRow(Row); 
      } 
      else 
      { 
       DisplayRow(Row); 
      } 
     </tr>*@ 
     //Row.CRUDOperation.Equals("Edit") ? EditRow(Row) : DisplayRow(Row); 
    } 
</table>  
} 

這裏是我的控制器

public class GridViewController : Controller 
{ 
    [HttpPost]   
    public ActionResult SubmitUpdateGridRow(FormCollection FC, SampleModel VM) 
    { 
     string str = (string)FC.GetValue("Row.Name").AttemptedValue; 
     ......    
    } 
} 

我能得到從形式收集的值,但我模型是空的。

在此先感謝。

PS:我想找到只與服務器端腳本的解決方案,不想使用JavaScript,JQuery的

+0

如何U [R從輸入型提交烏爾提交或輸入型按鈕? –

回答

0

在您的觀點中,您發佈了SampleModel的一個集合,但是您的控制器只需要一個參數SampleModel

首先,您需要更改控制器以接受IEnumerable<SampleModel>

然後在您的視圖中,您需要將索引傳遞給html幫助器,以生成正確的用於列表模型綁定的html,以便開箱即用。

例如:

@model IList<SampleModel> 

@for(var i = 0; i < Model.Count; i++) 
{ 
    @Html.HiddenFor(m => Model[i].Id) 
} 

檢查這個有用article更多關於模型綁定列表

+0

感謝您的幫助,它的工作。 但我不需要更改控制器,採取IEnumerable bcoz我只發送一個SampleModel對象到「插入」提交按鈕單擊控制器。 –

-1

放在控制器行動,你的HTML表單的頂部防僞屬性有防僞標籤。

+0

無法正常工作,仍然將模型設爲空 –