Asp.Net MVC 5 Html.DropDownList无法验证 [英] Asp.Net MVC 5 Html.DropDownList is not validating
问题描述
我已经在这个问题上停留了一段时间了,似乎找不到任何有用的在线信息.
I've been stuck on this problem for a while now and can't seem to find anything online that helps.
我在编辑器模板中有一个Html.DropDownList.我在表单中使用该模板,但没有为下拉列表选择任何内容.当我单击提交时,我希望表单通知我所需的下拉列表没有选择值,但没有选择.我究竟做错了什么?
I have a Html.DropDownList in an Editor Template. I use that template in my form and don't select anything for the dropdownlist. When I click submit, I expect the form to notify me that the required dropdownlist doesn't have a value selected, but it doesn't. What am I doing wrong?
以下是视图模型:
public class RequestCreateViewModel
{
public int ID { get; set; }
public TesterLocationCreateViewModel TesterLocationCreateViewModel { get; set; }
....
public RequestCreateViewModel()
{
}
}
public class TesterLocationCreateViewModel
{
public int ID { get; set; }
[Required]
[UIHint("OEM")]
public string OEM { get; set; }
[Required]
[DisplayName("City")]
public string LocationCity { get; set; }
public TesterLocationCreateViewModel()
{
}
}
这是Create.cshtml的代码段
Here's a snippet of the Create.cshtml
@model WdcTesterManager.Models.Request
@{
ViewBag.Title = "Create Request";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create Request</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.EditorFor(model => model.TesterLocationCreateViewModel)
</div>
</div>
}
这是TesterLocationCreateViewModel.cshtml(编辑器模板):
Here's the TesterLocationCreateViewModel.cshtml (Editor Template):
@model WdcTesterManager.Models.TesterLocationCreateViewModel
<div class="col-md-6">
<h4>Tester Location</h4>
<div class="container-fluid">
<div class="form-group">
@Html.LabelFor(model => model.OEM, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.DropDownList("OEM", (SelectList)ViewBag.OemList, "Choose one", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.OEM, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LocationCity, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.LocationCity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LocationCity, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
当我提交表格而没有填写任何内容时,我得到了纽约市的验证错误,但没有收到OEM的验证错误.有什么想法吗?
When I submit the form without filling anything out, I get a validation error for the City but nothing for the OEM. Any ideas?
推荐答案
@StephenMuecke指出代码看起来有些问题,因为代码似乎是在引用请求模型而不是请求视图模型.
@StephenMuecke pointed out that something looked wrong with the code, since the code seemed to be referencing the Request model rather than the Request View model.
因此,我再次查看了代码,并意识到Create.cshtml使用的是Request模型而不是RequestCreateViewModel.
So, I took a second look at the code and realized that the Create.cshtml was using the Request model rather than the RequestCreateViewModel.
将Create.cshtml更改为使用RequestCreateViewModel后,我得到了正确的验证错误:
I'm getting the correct validation errors after changing the Create.cshtml to use the RequestCreateViewModel:
@model WdcTesterManager.Models.RequestCreateViewModel
这篇关于Asp.Net MVC 5 Html.DropDownList无法验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!