C#MVC - View在Postback上返回Model null

时间:2014-09-25 14:07:30

标签: c# asp.net asp.net-mvc

我完全不知道为什么在提交表单后模型为空.....我已经检查了 FormCollection ,但它只包含" ScriptAssignments.Assigned" 。我正在使用一个强类型的视图,我正在使用一个beginform ....我不知道这里有什么问题......我将不胜感激任何帮助。

这就是View的样子......

@model Models.ScriptAssignmentMatrix

@{
   ViewBag.Title = "Script Matrix";
}

<link href="@Url.Content("~/Content/RotatedHeaders.css")" rel="stylesheet" type="text/css" />

<h1>Assignment Matrix</h1>

@using (Html.BeginForm("SaveChanges", "ScriptMatrix",FormMethod.Post)) {
<div style=" height:15px"></div>    
<table class="table table-striped table-header-rotated">
   <thead>
      <tr>
        <th></th> 
        @foreach (var header in Model.ColumnHeaders) 
        {
            <th>
                <div class="container"><div class="head"><div class="vert">
                    <span style="font-size: 10pt">
                        @Html.DisplayFor(modelItem => header)
                    </span>
                </div></div></div>
            </th>
        }
      </tr>
    </thead>

    <tbody>

    @foreach (var row in Model.RowHeaders)
    {
        <tr>
            <td>
                <span style="font-size: 10pt; background: #eee; ">
                    @Html.DisplayFor(modelItem => row)
                </span>
            </td>
            @foreach (var header in Model.ColumnHeaders)
            {
                var ScriptAssignments = (from y in Model.ScriptAssignments
                               where y.AgentName == row && y.ScriptName == header
                               select y).First();
                <td>     
                    @Html.CheckBoxFor(Assignment => ScriptAssignments.Assigned) 
                </td>
            }
        </tr>

    }
    </tbody>

</table>

<div style=" height:30px"></div>    
<input type="submit" value="Save Changes" />
<div style=" height:30px"></div>    
 @ViewBag.Result

}

这是控制器:

    [HttpPost]
    public ViewResult SaveChanges(ScriptAssignmentMatrix model)
    {

        ViewBag.Result = "Your changes have been saved";
        return View("Index", model);
    }

这是ScriptAssignmentMatrix类:

public class ScriptAssignmentMatrix
{
    public List<ScriptAssignmentMatrixElement> ScriptAssignments { get; set; }
    public List<String> ColumnHeaders { get; set; }
    public List<String> RowHeaders { get; set; }
}

public class ScriptAssignmentMatrixElement
{
    public int ScriptId { get; set; }
    public string  ScriptName { get; set; }

    public int AgentId { get; set; }
    public string AgentName { get; set; }

    public bool Assigned { get; set; }
}

2 个答案:

答案 0 :(得分:1)

DisplayFor不会将控件与模型绑定。所以最好的方法是使用HiddenFor。

@foreach (var header in Model.ColumnHeaders) 
{
    <th>
        <div class="container"><div class="head"><div class="vert">
            <span style="font-size: 10pt">
                @Html.DisplayFor(modelItem => header)
                @Html.HiddenFor(modelItem => header)
            </span>
        </div></div></div>
    </th>
}

答案 1 :(得分:0)

所以我终于找到了答案....仍然无法解释为什么.....但这是视图的工作修改代码.....

感谢gunr2171和Jorge F ......你的评论让我走在正确的轨道上.....

Models.ScriptAssignmentMatrix

@{
    ViewBag.Title = "Script Matrix";
}

<link href="@Url.Content("~/Content/RotatedHeaders.css")" rel="stylesheet" type="text/css" />

<h1>Assignment Matrix</h1>

@using (Html.BeginForm("SaveChanges", "ScriptMatrix",FormMethod.Post)) {

<div style=" height:15px"></div>    
<table class="table table-striped table-header-rotated">
   <thead>
      <tr>
        <th></th> 
        @foreach (var header in Model.ColumnHeaders) 
        {
            <th>
                <div class="container"><div class="head"><div class="vert">
                    <span style="font-size: 10pt">
                        @Html.DisplayFor(modelItem => header)
                    </span>
                </div></div></div>
            </th>
        }
      </tr>
    </thead>

    <tbody>

    @for (int row = 0; row < Model.RowHeaders.Count; row++ )
    {
        <tr>
            <td>
                <span style="font-size: 10pt; background: #eee; ">
                    @Html.DisplayFor(modelItem => Model.RowHeaders[row])
                </span>
            </td>
            @for (int column = 0; column < Model.ColumnHeaders.Count; column++ )
            {

                for (int index = 0; index < Model.ScriptAssignments.Count; index++)
                {
                     if (Model.ScriptAssignments[index].AgentName == Model.RowHeaders[row] && Model.ScriptAssignments[index].ScriptName == Model.ColumnHeaders[column])
                     {
                        <td>     
                            @Html.CheckBoxFor(Assignment =>  Model.ScriptAssignments[index].Assigned) 
                            @Html.HiddenFor(x =>  Model.ScriptAssignments[index].AgentId)
                            @Html.HiddenFor(x =>  Model.ScriptAssignments[index].ScriptId)
                            @Html.HiddenFor(x =>  Model.ScriptAssignments[index].AgentName)
                            @Html.HiddenFor(x =>  Model.ScriptAssignments[index].ScriptName)
                        </td>                             
                         break;
                     }

                }                    
            }
        </tr>
    }
    </tbody>

</table>

<div style=" height:30px"></div>    

<input type="submit" value="Save Changes" />
<div style=" height:30px"></div>    
 @ViewBag.Result

}

相关问题