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

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

问题描述

正如问题所说:如何在 DropDownListFor Html helper 中设置 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 并根据属性值设置 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

我还建议您的视图模型包含一个属性 IEnumerableTipoviDepozita 并在控制器中生成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天全站免登陆