HTML辅助DropDownList的解释 [英] HTML helper DropDownList explained

查看:206
本文介绍了HTML辅助DropDownList的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人请解释这是如何工作? (这是使用过载)

Can someone please explain how this works? (which overload is being used)

@Html.DropDownList("FinancialYearID", "Select FY")

FinancialYearID是鉴于袋的SelectList。为什么它作为一个字符串传递?

FinancialYearID is a SelectList in view bag. Why is it passed as a string?

另外我怎么能添加的输入元素自定义属性?
添加第三个参数

Also how can I add custom attributes on the input element? Adding a 3rd parameter

new { @class = "foo" } 

不起作用?

是否可以使用默认的助手添加数据 - 属性?

Is it possible to add data- attributes using the default helpers?

感谢

推荐答案

您所呼叫的这个超载​​。这将创建与名称的FinancialYearID(这意味着选择的值将绑定到一个名为FinancialYearID模式或ViewBag财产)和选择风云作为占位符(空)选择一个下拉列表文字。

You're calling this overload. This creates a dropdown list with the name "FinancialYearID" (which means that the selected value will bind to a model or ViewBag property called "FinancialYearID") and "Select FY" as the placeholder (empty) selection text.

如果你想添加一个类,你需要的下拉(字符串,IEnumerable的,字符串对象)助手:

If you want to add a class, you need the DropDown(string, IEnumerable, string, object) helper:

@Html.DropDownList("FinancialYearID", null, "Select FY", new { @class = "foo" })

在这里,你还可以通过传递一个的IEnumerable (如的SelectList )其中填充DropDownList的我已通过

Here, you can also populate the DropDownList by passing in an IEnumerable (such as a SelectList) where I have passed null.

是的,这是完全有可能通过数据属性。只是用下划线代替连字符:

Yes, it is entirely possible to pass a data attribute. just replace the hyphen with an underscore:

@Html.DropDownListFor("FinancialYearID", null,
                   "Select FY", new { @data_something = "foo" })

如果你有一个模型属性它你想选择的值绑定,就可以通过一个兰巴前pression,表明要绑定和框架会生成相应的<$ c中的财产$ C>名称:

If you have a model property to which you're trying to bind the selected value, you can pass a lamba expression to indicate the property that you want to bind and the framework will generate the appropriate name:

@Html.DropDownList(m => m.FinancialYearID, MySelectList,
                   "Select FY", new { @data_something = "foo" })

我想也通常建议把的SelectList 模型的初始GET请求,而不是使用 ViewBag

I'd also generally advise putting the SelectList in the model on the initial GET request rather than using ViewBag.

这篇关于HTML辅助DropDownList的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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