【发布时间】:2017-10-03 19:53:23
【问题描述】:
我有一个视图模型,视图模型的属性之一是一个名为 Profile 的对象。 Profile 的属性之一是另一个对象的列表,称为 CD。从视图中,我将 POST 正文值设置为以下
Profile.CD[0].Prop1=TEST&Profile.CD[0].Prop2=TEST&Profile.CD[1].Prop1=TEST2&Profile.CD[1].Prop2=TEST2
如果我要在视图中的列表中添加第三个对象,它将被发布为 Profile.CD[2].Prop1=TEST3 ,在控制器中突然 Profile.CD 为空。 2 项及以下Profile.CD 获得我期望的值。一旦我添加了第三个项目,模型活页夹就会停止工作。我不知所措,我已经尝试了我能想到的一切。
我尝试过的事情
从视图中移除一个项目,并添加一个新的 -- WORKS
从视图中删除这两个项目,并添加 2 个新项目 -- WORKS
在视图中添加第三项 -- FAILS
Profile.CD在视图模型中为空
我没有使用任何模型状态验证规则。调试时,我在即时窗口?Request.Form.Where(x => x.Keys.Contain("Profile.CD")).ToList()) 中尝试了类似以下的操作,果然,我的所有值都存在于Request 对象中,即使视图模型中的列表为空。
Profile.CD 中对象的值不必是唯一的。我尝试将每个值都设置为“TEST”,只是为了验证它不是导致此问题的输入。
我真的迷路了..
查看模型
public class PortalViewModel {
public ProfileModel Profile { get; set; }
}
个人资料模型
public class ProfileModel {
//bunch of other static properties that are bound just fine.. like strings and decimals...
public List<CDModel> CD { get; set; }
}
控制器
public async Task<IActionResult> Edit (PortalViewModel Model)
{
Repo.UpdateProfile(Model.Profile); // in this method it foreachs Profile.CD , but sometimes this is null and other times it get's it's values....
return Ok();
}
【问题讨论】:
标签: c# asp.net-core model-binding