我为什么这不起作用而感到茫然。
这是我模特的一部分:
public class AddComplianceReportViewModel
{
public PartyViewModel[] InvolvedParties { get; set; }
public PartyViewModel[] ManagementParties { get; set; }
public ConcealPartyViewModel[] ConcealParties { get; set; }
public string ReportedByFirstName { get; set; }
public string ReportedByLastName { get; set; }
public string ReportedByBestTimeToContact { get; set; }
public bool ReportedByRemainAnonymous { get; set; }
[Phone]
public string ReportedByPhone { get; set; }
[EmailAddress]
public string ReportedByEmail { get; set; }
public string GeneralDescription { get; set; }
public string DetaledDescription { get; set; }
}
public class PartyViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
}
public class ConcealPartyViewModel : PartyViewModel
{
public string ConcealmentDescription { get; set; }
}
来自我的控制器的相关代码:
[HttpGet, Route("report/new")]
public ViewResult FileReport()
{
var vm = new AddComplianceReportViewModel()
{
ReportedByFirstName = "amy",
ReportedByLastName = "lastname",
ReportedByEmail = "my@email.com",
ReportedByBestTimeToContact = "best time",
ReportedByPhone = "555-123-4567",
ReportedByRemainAnonymous = false,
InvolvedParties = new []
{
new PartyViewModel()
{
FirstName = "saul",
LastName = "goodman",
Title = "lawyer"
},
new PartyViewModel()
{
FirstName = "walter",
LastName = "white",
Title = "cook"
},
new PartyViewModel()
{
FirstName = "jesse",
LastName = "pinkman",
Title = "lackey"
}
},
ManagementParties = new PartyViewModel[]
{
new PartyViewModel()
{
FirstName = "Gus",
LastName = "Freng",
Title = "Boss"
}
},
ConcealParties = new ConcealPartyViewModel[]
{
new ConcealPartyViewModel()
{
FirstName = "Skyler",
LastName = "White",
Title = "Wife",
ConcealmentDescription = "money laundering"
},
new ConcealPartyViewModel()
{
FirstName = "Mike",
LastName = "Ehrmantraut",
Title = "Enforcer",
ConcealmentDescription = "hid the bodies"
}
},
GeneralDescription = "general description",
DetaledDescription = "detailed description"
};
return View(vm);
}
[HttpPost, Route("report/new", Name = RouteNames.PostReport)]
public ActionResult FileReport(AddComplianceReportViewModel vm)
{
return View(vm);
}
我不知道是否需要分享观点。有很多视图代码可以削减相关位。如果需要,我可以添加我的观点。
这是我的浏览器发送到上面第二个控制器方法的表单数据:
ReportedByFirstName =艾米&安培; ReportedByLastName = latname&安培; ReportedByPhone =%28555%29 + 123-4567&安培; ReportedByEmail =我%40email.com&安培; ReportedByBestTimeToContact =最好+时间&安培;%5B0%5D.FirstName =索尔&安培;%5B0%5D。名字=古德曼&安培;%5B0%5D.Title =律师&安培;%5B1%5D.FirstName =沃尔特&安培;%5B1%5D.LastName =白色&安培;%5B1%5D.Title =煮&安培;%5B2%5D.FirstName =杰西&安培;% 5B2%5D.LastName = pinkman&安培;%5B2%5D.Title =走狗&安培;%5B0%5D.FirstName =格斯&安培;%5B0%5D.LastName = Freng&安培;%5B0%5D.Title =老板&安培; GeneralDescription =一般+描述&安培;持续时间=安培; AwarenessReason =安培; AwarenessReason =安培;%5B0%5D.FirstName =斯凯勒和放大器;%5B0%5D.LastName =白色和放大器;%5B0%5D.Title =妻子和放大器;%5B0%5D.ConcealmentDescription =金钱+洗钱和放大器; %5B1%5D.FirstName =麦克&安培;%5B1%5D.LastName = Ehrmantraut&安培;%5B1%5D.Title =强制实施&安培;%5B1%5D.ConcealmentDescription = HID +的+体
这是不可能阅读的,所以这里是由Fiddler提供的图像(并非所有字段都适合图像):
如下图所示,没有任何集合正在填充:
我需要做些什么来填充这些?我不知所措。我在Fiddler截图中注意到派对收藏品含糊不清。我怎么告诉MVC"这个组是ConcealedParties,这个组是ManagementParties"等?
==================每个请求,意见================
_Parties.cshtml
@model Redacted.Web.Models.Home.PartyViewModel[]
<div class="row">
<div>@Html.Label("First Name")</div>
<div>@Html.Label("Last Name")</div>
<div>@Html.Label("Title")</div>
</div>
@for (var i = 0; i < Model.Count(); i++)
{
<div class="row">
<div class="columns medium-1"> </div>
<div class="columns medium-3">
@Html.EditorFor(model => model[i].FirstName)
</div>
<div class="columns medium-3">
@Html.EditorFor(model => model[i].LastName)
</div>
<div class="columns medium-3">
@Html.EditorFor(model => model[i].Title)
</div>
</div>
}
主要观点
@using (Html.BeginForm("FileReport", "Home", FormMethod.Post))
{
@Html.Partial("_Parties", Model.InvolvedParties ?? new PartyViewModel[] { })
}
ManagementParties
集合以相同的方式完成。 ConcealedParties
集合具有不同的局部视图,但部分以相同的方式加载。
答案 0 :(得分:2)
您需要将部分视图发送到整个主模型,然后使用该模型访问字段。由于您没有这样做,因此您的字段名称无法正确形成,因此模型绑定无法做到这一点。
请参阅此处(来自您的表单数据):
[0].FirstName
应该是:
InvolvedParties[0].FirstName
但它不能以InvolvedParties
作为前缀,因为您的部分视图认为顶级模型本身就是列表,因此遗漏了InvolvedParties
。
在模型中阻止null
集合的另一个提示,只需使用coalesce在模型构造函数中初始化它们:
public AddComplianceReportViewModel ()
{
InvolvedParties = InvolvedParties ?? new PartyViewModel[] { };
}