Asp.Net MVC 5 Html.DropDownList is not validating

有些话、适合烂在心里 提交于 2019-12-12 01:37:34

问题


I've been stuck on this problem for a while now and can't seem to find anything online that helps.

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?

Here are the View Models:

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()
    {

    }
}

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>
 }

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>

When I submit the form without filling anything out, I get a validation error for the City but nothing for the OEM. Any ideas?


回答1:


@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.

So, I took a second look at the code and realized that the Create.cshtml was using the Request model rather than the RequestCreateViewModel.

I'm getting the correct validation errors after changing the Create.cshtml to use the RequestCreateViewModel:

@model WdcTesterManager.Models.RequestCreateViewModel


来源:https://stackoverflow.com/questions/36806278/asp-net-mvc-5-html-dropdownlist-is-not-validating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!