开发者

MVC DropDownListFor() Selected Item is not Selected / Required Validation not run

开发者 https://www.devze.com 2023-02-18 16:46 出处:网络
I am having trouble getting my DropDownList to set the selected item to the value from the model. The field in the model is just a string for the Title of the users name (Mr, Miss etc.开发者_Python百

I am having trouble getting my DropDownList to set the selected item to the value from the model.

The field in the model is just a string for the Title of the users name (Mr, Miss etc.开发者_Python百科.) Below is my code so far.

<td>
@{ var list = new List<SelectListItem>(new[] {                    
    new SelectListItem{ Selected = string.IsNullOrEmpty(Model.Title), Text="",Value=""},
    new SelectListItem{ Selected = Model.Title.Equals("Mr"), Text="Mr",Value="Mr"},
    new SelectListItem{ Selected = Model.Title.Equals("Mrs"),   Text="Mrs",Value="Mrs"},
    new SelectListItem{ Selected = Model.Title.Equals("Miss"), Text="Miss",Value="Miss"},
    new SelectListItem{Selected = Model.Title.Equals("Ms"), Text="Ms",Value="Ms"}       
    });
}
@Html.DropDownListFor(m=>m.Title, list)
</td>


I had this problem with MVC 3 and it turned out that I had set ViewBag.Title on my View (using it for the page title). As soon as I changed it to ViewBag.PageTitle, the dropdownlist code started working : @Html.DropDownListFor(model => model.Title, Model.MySelectList)

The reason for this is that in MVC 2/3, any ViewBag / ViewData properties with the same name as those in the Model object get used in preference in DropDownListFor(), so you need to rename them to make sure they don't conflict. Because that seems really flaky, I just stopped using ViewBag entirely and now rely only on the View Model for passing stuff into the View.

The reason this problem is so prevalent is that ViewBag.Title is used in many introductory tutorials and demo code to set the HTML title element, and so inevitably gets adopted as a "best-practice" approach. However, Title is a natural Model property name for use in dropdowns on a "User Details" view.


So it turns out that the only reason it doesn't work is because my field name is Title, I changed it to Prefix and my exact code works. Way too much time spent finding that out...

Here is working code.

<td>
    @{ var list = new List<SelectListItem>(new[] {
        new SelectListItem { 
            Selected = string.IsNullOrEmpty(Model.Prefix), 
            Text="",
            Value=""
        },
        new SelectListItem { 
            Selected = Model.Prefix.Equals("Mr"), 
            Text="Mr",
            Value="Mr"
        },
        new SelectListItem {
            Selected = Model.Prefix.Equals("Mrs"),
            Text="Mrs",
            Value="Mrs"
        },
        new SelectListItem {
            Selected = Model.Prefix.Equals("Miss"), 
            Text="Miss",
            Value="Miss"
        },
        new SelectListItem {
            Selected = Model.Prefix.Equals("Ms"), 
            Text="Ms",
            Value="Ms"
        }       
      });
    }
    @Html.DropDownListFor(m => m.Prefix, list)
</td>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号