Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MVC-视图在回发时返回模型null_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C#MVC-视图在回发时返回模型null

C#MVC-视图在回发时返回模型null,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我完全不知道为什么提交表单后模型为空……我检查了表单集合,但它只包含“ScriptAssignments.Assigned”。我使用的是强类型视图,我使用的是beginform…我不明白这里有什么错…我将感谢您的帮助 这就是视图的外观…… @model Models.ScriptAssignmentMatrix @{ ViewBag.Title = "Script Matrix"; } <link href="@Url.Content("~/Content/RotatedHead

我完全不知道为什么提交表单后模型为空……我检查了表单集合,但它只包含“ScriptAssignments.Assigned”。我使用的是强类型视图,我使用的是beginform…我不明白这里有什么错…我将感谢您的帮助

这就是视图的外观……

@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
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
这是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; }
}
公共类ScriptAssignmentMatrix
{
公共列表脚本分配{get;set;}
公共列表列标题{get;set;}
公共列表行标题{get;set;}
}
公共类ScriptAssignmentMatrixElement
{
public int ScriptId{get;set;}
公共字符串ScriptName{get;set;}
公共int代理{get;set;}
公共字符串AgentName{get;set;}
公共布尔赋值{get;set;}
}

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>
}
@foreach(Model.ColumnHeaders中的变量头)
{
@DisplayFor(modeleItem=>header)
@HiddenFor(modeleItem=>header)
}

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

谢谢gunr2171和Jorge F....你的评论让我走上了正确的道路。

@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
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
Models.ScriptAssignmentMatrix
@{
ViewBag.Title=“脚本矩阵”;
}
分配矩阵
@使用(Html.BeginForm(“SaveChanges”、“ScriptMatrix”、FormMethod.Post)){
@foreach(Model.ColumnHeaders中的var头)
{
@DisplayFor(modeleItem=>header)
}
@对于(int row=0;rowModel.RowHeaders[row])
@对于(int column=0;columnModel.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)
打破
}
}                    
}
}
@查看包。结果

}

foreach的
可能有问题。改为使用
for
循环。请参阅。
DisplayFor
不会创建
input
元素。要修复它,请在每次
DisplayFor
之后使用
HiddenFor
,DisplayFor仅用于表头…需要保存在模型中的数据在嵌套的foreach中。我在嵌套的foreach中使用CheckboxFor的几次经验表明,它的工作方式是使用for循环@gunr2171注释。试一试。如果不使用
HiddenFor
字段发布标题值,则可能无法正确进行模型绑定。MVC喜欢在post绑定过程中返回所有模型字段。尝试添加隐藏字段,看看会发生什么。