发布到模型的 MVC DropDownList 值未绑定
DropDownList 可能是我在使用 MVC 框架时最不喜欢的部分。我的表单中有几个下拉菜单,我需要将其选定的值传递给接受模型作为参数的 ActionResult。
标记看起来像这样:
<div class="editor-label">
@Html.LabelFor(model => model.FileType)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.FileType.Variety, (SelectList)ViewBag.FileTypes)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Status.Status, (SelectList)ViewBag.Status)
</div>
我的控制器操作看起来像这样:
[HttpPost]
public ActionResult Create(int reviewid, ReviewedFile file)
{
if (ModelState.IsValid)
{
UpdateModel(file);
}
//repository.Add(file);
return RedirectToAction("Files", "Reviews", new { reviewid = reviewid, id = file.ReviewedFileId });
}
这应该一切都很好,除了下拉列表中的值被发布为 null 之外。当我进一步查看 ModelState 错误时,发现原因是:
参数类型转换 要输入的“System.String” 'PeerCodeReview.Models.OutcomeStatus' 失败,因为没有类型转换器可以 这些类型之间进行转换。
这不应该这么难,但事实就是如此。所以问题是;我需要做什么才能正确绑定我的模型属性?
顺便说一句,我知道我可以传入 FormCollection 对象,但这意味着更改当前需要强类型模型参数的单元测试的重要部分。
DropDownLists are probably my least favourite part of working with the MVC framework. I have a couple of drop-downs in my form whose selected values I need to pass to an ActionResult that accepts a model as its parameter.
The markup looks like this:
<div class="editor-label">
@Html.LabelFor(model => model.FileType)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.FileType.Variety, (SelectList)ViewBag.FileTypes)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Status.Status, (SelectList)ViewBag.Status)
</div>
And my controller action looks like this:
[HttpPost]
public ActionResult Create(int reviewid, ReviewedFile file)
{
if (ModelState.IsValid)
{
UpdateModel(file);
}
//repository.Add(file);
return RedirectToAction("Files", "Reviews", new { reviewid = reviewid, id = file.ReviewedFileId });
}
This should be all well and good except the values from the drop downs are being posted as null. When I look further into the ModelState errors, the cause is found to be:
The parameter conversion from type
'System.String' to type
'PeerCodeReview.Models.OutcomeStatus'
failed because no type converter can
convert between these types.
It shouldn't be this hard, but it is. So the question is; what do I need to do in order to get my model properties bound correctly?
As an aside, I know I could pass in a FormCollection object, but that means changing significant parts of my unit tests that currently expect a strongly-typed model parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要为绑定到下拉列表的两个属性创建并注册自定义模型绑定器。
以下是我为此目的构建的模型绑定程序的代码:
TModel
是下拉框应绑定到的属性的类型。在我的应用程序中,下拉框提供了数据库中对象的 ID,因此该模型绑定器采用该 ID,从数据库中检索正确的实体,然后返回该实体。您可能有不同的方法将下拉列表给出的字符串转换为模型的正确实体。您还需要在 Global.asax 中注册模型绑定器。
这假设下拉列表控件的名称与类型名称相同。如果没有,您可以将密钥传递给模型绑定器。
You need to create and register a custom model binder for the two properties that are bound to the drop-down lists.
Here's my code for a model binder I built for exactly this purpose:
TModel
is the type of the property that the drop-down box should bind to. In my app the drop-down box gives the Id of the object in the database, so this model binder takes that id, retrieves the correct entity from the database, then returns that entity. You may have a different way to convert the string given by the drop-down to the correct entity for the model.You also need to register the model binder in
Global.asax
.This assumes that the name of the drop-down list control is the same as the type name. If not, you can pass a key to the model binder.
试试这个:
Try this instead: