一个特定 ViewModel 的 ModelBinder returns null
ModelBinder returns null for one particular ViewModel
我知道,有很多问题,尝试了解决方案,但都没有用。基本上,我卡住了...
ViewModel:
public class CreateStrengthViewModel
{
public string Strength { get; set; }
public IEnumerable<Categories.CategoryViewModel> Categories { get; set; }
public int Category { get; set; }
}
Post:
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="__RequestVerificationToken"
iErDomlK5vCWAFFMlGkb2-HgLCoquxfeIlYeI3pmrsW_5VSD8-huS6JbCdA4OAg4s8nMgqKAHPHArVoQ3GzfFWf2I-Yx6iWgvkWRNI6jiKA1
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="Strength"
FMC Extra
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="Category"
1
-----------------------------7e01e1381a08c2--
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include = "Strength,Category")]
CreateStrengthViewModel strength)
{
using (StrengthServices service = new StrengthServices(new ImperialTobacco_Database()))
{
//service.CreateAndSave(strength);
...
}
}
}
无论我在做什么,控制器方法中的强度都是空的。如您所见,浏览器发送数据,但 ModelBinder 什么也不做。哦,顺便说一句:相同的代码适用于具有不同 Include 字段的不同 ViewModel。是的,我试过删除绑定,或者只绑定一个传入变量,没有任何变化。
有趣的是;这是有效的:
public class CreateCompanyViewModel
{
public string Name { get; set; }
public string Color { get; set; }
public IEnumerable<MarketViewModel> MarketsOfOperations { get; set; }
public IEnumerable<int> SelectedOperations { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Name,Color,SelectedOperations")] CreateCompanyViewModel company)
{
using (CompanyServices service = new CompanyServices(new ImperialTobacco_Database()))
{
service.CreateAndSave(company);
return RedirectToAction("Index", "Management");
}
}
老实说,我不知道发生了什么,也不知道为什么 ModelBinder 没有从发布的数据中提取任何东西,尤其是当一个几乎相同的数据完美运行时。
P.S.: 我尝试更改 属性 和绑定名称,但还是失败了。
问题是您的模型中有一个 属性 名称与操作方法中的参数名称匹配。
因此,如果您将操作名称参数从
public ActionResult Create([Bind(Include = "Strength,Category")] CreateStrengthViewModel strength)
到
public ActionResult Create([Bind(Include = "Strength,Category")] CreateStrengthViewModel strengthModel)
这将解决它
我知道,有很多问题,尝试了解决方案,但都没有用。基本上,我卡住了...
ViewModel:
public class CreateStrengthViewModel
{
public string Strength { get; set; }
public IEnumerable<Categories.CategoryViewModel> Categories { get; set; }
public int Category { get; set; }
}
Post:
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="__RequestVerificationToken"
iErDomlK5vCWAFFMlGkb2-HgLCoquxfeIlYeI3pmrsW_5VSD8-huS6JbCdA4OAg4s8nMgqKAHPHArVoQ3GzfFWf2I-Yx6iWgvkWRNI6jiKA1
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="Strength"
FMC Extra
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="Category"
1
-----------------------------7e01e1381a08c2--
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include = "Strength,Category")]
CreateStrengthViewModel strength)
{
using (StrengthServices service = new StrengthServices(new ImperialTobacco_Database()))
{
//service.CreateAndSave(strength);
...
}
}
}
无论我在做什么,控制器方法中的强度都是空的。如您所见,浏览器发送数据,但 ModelBinder 什么也不做。哦,顺便说一句:相同的代码适用于具有不同 Include 字段的不同 ViewModel。是的,我试过删除绑定,或者只绑定一个传入变量,没有任何变化。
有趣的是;这是有效的:
public class CreateCompanyViewModel
{
public string Name { get; set; }
public string Color { get; set; }
public IEnumerable<MarketViewModel> MarketsOfOperations { get; set; }
public IEnumerable<int> SelectedOperations { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Name,Color,SelectedOperations")] CreateCompanyViewModel company)
{
using (CompanyServices service = new CompanyServices(new ImperialTobacco_Database()))
{
service.CreateAndSave(company);
return RedirectToAction("Index", "Management");
}
}
老实说,我不知道发生了什么,也不知道为什么 ModelBinder 没有从发布的数据中提取任何东西,尤其是当一个几乎相同的数据完美运行时。
P.S.: 我尝试更改 属性 和绑定名称,但还是失败了。
问题是您的模型中有一个 属性 名称与操作方法中的参数名称匹配。
因此,如果您将操作名称参数从
public ActionResult Create([Bind(Include = "Strength,Category")] CreateStrengthViewModel strength)
到
public ActionResult Create([Bind(Include = "Strength,Category")] CreateStrengthViewModel strengthModel)
这将解决它