C# 子模型属性的值始终为空

C# 子模型属性的值始终为空,c#,model-view-controller,C#,Model View Controller,我正在使用视图生成隐藏字段,但该值始终为空。这是我的密码。不知道怎么了。我有包含Id和Name属性的FillRequest列表。它是子模型的一部分 @using Computs.Models @model RequestViewModel @using (Html.BeginForm(null, null, FormMethod.Post, id = "PostForm", class = "form-horizontal" )) { foreach(v

我正在使用视图生成隐藏字段,但该值始终为空。这是我的密码。不知道怎么了。我有包含Id和Name属性的FillRequest列表。它是子模型的一部分

@using Computs.Models
@model RequestViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, id = "PostForm", class = "form-horizontal" ))
{
 foreach(var reqest in Model.FillRequest)
 {
  @Html.HiddenFor(m=>reqest.Id)
  @Html.HiddenFor(m=>reqest.Name)
 }
 <button class="btn btn-default">Save</button>
}
@使用Computs.Models
@模型请求视图模型
@使用(Html.BeginForm(null,null,FormMethod.Post,id=“PostForm”,class=“form horizontal”))
{
foreach(模型中的var REQUEST.FillRequest)
{
@HiddenFor(m=>request.Id)
@Html.HiddenFor(m=>request.Name)
}
拯救
}
检查控制器中的post方法后,Id和Name属性始终为空

这是模型

public class RequestViewModel
{
    public String TypeOfRequest { get; set; }
    public List<FillRequest> FillRequests { get; set; }
} 
公共类RequestViewModel
{
公共字符串类型请求{get;set;}
公共列表请求{get;set;}
} 
这是我的控制器获取方法

    public ActionResult Index()
    {
        var requestModel = new RequestViewModel();
        requestModel.FillRequests = new List<FillRequest>();
        requestModel.FillRequests.add(new FillRequest() { Id = 1, Name = "John"});
        requestModel.FillRequests.add(new FillRequest() { Id = 2, Name = "Terry"}); 

        return View(requestModel);
         
    }
public ActionResult Index()
{
var requestModel=new RequestViewModel();
requestModel.FillRequests=新列表();
添加(新的FillRequest(){Id=1,Name=“John”});
requestModel.FillRequests.add(新的FillRequest(){Id=2,Name=“Terry”});
返回视图(requestModel);
}

对于每个循环,不允许将@Html.HiddenFor()中的lambda绑定到模型元素。请注意,您传递参数“m”,但不要使用它:

foreach(var reqest in Model.FillRequest)
{
   @Html.HiddenFor(m=>reqest.Id)
   @Html.HiddenFor(m=>reqest.Name)
}
而是使用For循环:

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

如果看不到如何准备返回到视图的模型以及如何在视图中声明该模型,则很难说明问题。我回答了你的问题,但事后看来,你应该得到的是错误,而不是空值。您的模型的列表属性是
FillRequests
,复数,但您可以实例化它,然后填充单数
FillRequests
。即使这不是问题所在,我相信您的循环逻辑也应该创建一个错误,而不是返回null。您是否尝试创建一个简化的示例,但没有检查简化的代码是否有效?即使如此,我希望我的回答能帮助你。