在ASP.NET MVC之后的表单中没有填充模型

时间:2014-06-28 22:35:40

标签: asp.net-mvc razor

我正在学习ASP.NET MVC。我试图在表单帖子上捕获模型数据,但模型显示为null。

这是我的模特

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

将Antiforgery属性放在控制器操作之上,因为HTML表单包含Antiforgery标记。