MVC5-如何设置"selectedValue"在DropDownListFor Html助手中 [英] MVC5 - How to set "selectedValue" in DropDownListFor Html helper

查看:134
本文介绍了MVC5-如何设置"selectedValue"在DropDownListFor Html助手中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如问题所述: 如何在DropDownListFor HTML帮助程序中设置selectedValue?

As the question says: How to set selectedValue in DropDownListFor Html helper?

尝试了大多数其他解决方案,但都无济于事,这就是为什么我要提出一个新问题.

Tried most of the other solutions but none worked that's why I am opening a new question.

这些都无济于事:

@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2), htmlAttributes: new { @class = "form-control" })

//Not working with or without cast
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", (ProjectName.Models.TipDepozita)Model.TipoviDepozita.Single(x => x.Id == 2)), htmlAttributes: new { @class = "form-control" })
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", (ProjectName.Models.TipDepozita)Model.TipoviDepozita.Where(x => x.Id == 2).FirstOrDefault()), htmlAttributes: new { @class = "form-control" })

@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", new SelectListItem() { Value="2", Selected=true}), htmlAttributes: new { @class = "form-control" })

如果可能的话,我想避免手动创建SelectListItems或仅用于列表的ViewModel.

I would like to avoid manual creation of SelectListItems or a ViewModel just for the list if possible.

推荐答案

使用DropDownListFor()(或DropDownList())方法绑定到模型属性时,其属性值将设置选定的选项.

When you use the DropDownListFor() (or DropDownList()) method to bind to a model property, its the value of the property that sets the selected option.

内部,这些方法会生成自己的IEnumerable<SelectListItem>并根据属性的值设置Selected属性,因此将忽略代码中的Selected属性.仅当您不绑定到模型属性时(例如使用

Internally, the methods generate their own IEnumerable<SelectListItem> and set the Selected property based on the value of the property, and therefore setting the Selected property in your code is ignored. The only time its respected is when you do not bind to a model property, for example using

@Html.DropDownList("NotAModelProperty", new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2))

请注意,您可以检查源代码,特别是SelectInternal()GetSelectListWithDefaultValue()方法以了解其详细工作方式.

Note your can inspect the source code, in particular the SelectInternal() and GetSelectListWithDefaultValue() methods to see how it works in detail.

要在首次渲染视图时显示所选选项,请在将模型传递给视图之前,在GET方法中设置属性的值

To display the selected option when the view is first rendered, set the value of the property in the GET method before you pass the model to the view

我还建议您的视图模型包含属性IEnumerable<SelectListItem> TipoviDepozita,并在控制器中生成SelectList

I also recommend your view model contains a property IEnumerable<SelectListItem> TipoviDepozita and that you generate the SelectList in the controller

var model = new YourModel()
{
    TipoviDepozita = new SelectList(yourCollection, "Id", "Naziv"),
    TipPopustaId = 2 // set the selected option
}
return View(model);

因此视图变为

@Html.DropDownListFor(m => m.TipPopustaId, Model.TipoviDepozita, new { @class = "form-control" })

这篇关于MVC5-如何设置"selectedValue"在DropDownListFor Html助手中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆