EditorTemplate DropDown获取错误的ID并且无法进行验证

时间:2014-02-08 10:08:04

标签: validation asp.net-mvc-4 drop-down-menu mvc-editor-templates

我使用以下代码在MVC 4中创建了一个EditorTemplate:

@model Drexx.Models.Account
@Html.DropDownListFor(model => model.LevelId, (IEnumerable<SelectListItem>)ViewBag.LevelId, String.Empty)

我的视图有以下代码:

@Html.EditorForModel();

我的模型有这段代码:

[UIHint("LevelDropDown")]
[DisplayName("Level")]
public int LevelId { get; set; }
public virtual Level Level { get; set; }

My Controller在Get:

中包含此代码
ViewBag.LevelId = new SelectList(db.Level, "Id", "Name");

我的控制器在帖子中有这个代码:

ViewBag.LevelId = new SelectList(db.Level, "Id", "Name", account.LevelId);

这是我运行时源代码的样子:

<select data-val="true" data-val-number="The field Level must be a number." data-val-required="Feltet Level skal udfyldes." id="LevelId_LevelId" name="LevelId.LevelId">
    <option value=""></option>
    <option value="1">Normal</option>
    <option value="2">Admin</option>
</select>
<span class="field-validation-valid" data-valmsg-for="LevelId" data-valmsg-replace="true"></span>

为什么它将下拉列表的ID变为“LevelId_LevelId”并将名称变为“LevelId.LevelId”? 下拉列表工作并显示它应该是什么,但验证不起作用,因为id / name是错误的。验证突出显示下拉列表,但不显示错误消息。

在我开始使用EditorForModel

之前,将其移入EditorTemplate之前,下拉列表工作得很好

我是EditorTemplates的新手,所以很难搞清楚导致这种情况的原因。

2 个答案:

答案 0 :(得分:0)

这里似乎有些混乱。您已使用LevelId属性指向自定义UIHint编辑器模板修饰了LevelDropDown.cshtml整数属性,但您的编辑器模板似乎是强类型的Drexx.Models.Account模型不正确。

让我们举一个如何实现这一目标的例子。与ASP.NET MVC一样,您首先要设计视图模型,其中包含视图可能需要的必要属性:

public class AccountViewModel
{
    [DisplayName("Level")]
    public int LevelId? { get; set; }

    public IEnumerable<SelectListItem> Levels { get; set; }
}

然后你编写一个控制器动作来填充这个视图模型并将其传递给视图:

public ActionResult Index()
{
    var model = new AccountViewModel();
    model.LevelId = 123; // some value that you want to be used to preselect some element of the dropdown
    model.Levels = new SelectList(db.Level, "Id", "Name");

    return View(model);
}

然后在相应的视图中:

@model AccountViewModel
...
@Html.EditorForModel()

然后在~/Views/Shared/EditorTemplates/AccountViewModel.cshtml

@model AccountViewModel
@Html.DropDownListFor(x => x.LevelId, Model.Levels, string.Empty)

您还可以指定模板的名称:

@Html.EditorForModel("Foo")

将寻找~/Views/Shared/EditorTemplates/Foo.cshtml模板。

当您拥有更复杂的父视图模型时,可以使用UIHint属性。例如:

public class MyViewModel
{
    UIHint("LevelDropDown")
    public AccountViewModel Account { get; set; }

    ...
}

答案 1 :(得分:0)

我找到了解决方案。

模型应如下所示:

[DisplayName("Level")]
[UIHint("LevelId")]
[Required]
public int? LevelId { get; set; }

EditorTemplate应如下所示:

@model int?
@Html.DropDownListFor(model => model, (IEnumerable<SelectListItem>)ViewBag.LevelId, String.Empty)

我把“?”放在一边非常重要在int之后,因为我在下拉列表中有一个默认值。

我没有为下拉列表创建ViewModel。

这是源码最终照顾修复的方式:

<div class="editor-field">
    <select data-val="true" data-val-number="The field Level must be a number." data-val-required="Feltet Level skal udfyldes." id="LevelId" name="LevelId">
        <option value=""></option>
        <option value="1">Normal</option>
        <option value="2">Admin</option>
    </select>
    <span class="field-validation-valid" data-valmsg-for="LevelId" data-valmsg-replace="true"></span>
</div>

感谢Darin Dimitrov提出的所有建议,但他们并没有为我工作。