我知道,有很多問題,嘗試解決方案,他們都沒有工作。基本上,我卡...ModelBinder返回null爲一個特定的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不會執行任何操作。哦,順便說一句:這個代碼適用於不同的包含不同包含字段的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 .:我試着改變屬性和綁定的名字,但再次沒有運氣。
[Asp.Net MVC的可能重複:爲什麼是我的看法傳遞null模型回我的控制器?](http://stackoverflow.com/questions/34863167/asp-net-mvc-why-is-my-view-passing-null-models-back-to-my-controller) –