具有数据属性SelectListItem [英] SelectListItem with data-attributes

查看:997
本文介绍了具有数据属性SelectListItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正有ppopulated对视图模型与数据属性的的SelectList $ P $?

我想要做的。

  @ Html.DropdownListFor(M = GT; m.CityId,Model.Cities);

所以它生成code,如:

 <选择一个id =城市CLASS =location_city_inputNAME =城与GT;
    <选项数据的地理经纬度= - 32.522779数据地理LNG = - 55.765835数据地理变焦=6/>
    <STATE1选项数据的地理经纬度= - - 34.883611数据地理LNG =56.181944数据地理变焦=13数据的地理名称=蒙得维的亚数据子=值=1>&蒙得维的亚LT; /选项>
    <选项数据的地理经纬度= - 34.816667数据地理LNG = - 55.95数据地理变焦=13数据的地理名称=卡内洛内斯,城德拉科斯塔数据子= .state41VALUE =41>城德拉科斯塔< /选项>
< /选择>


解决方案

下面是简单的解决方案。

不是一切在.NET code扩展方法来写。一个关于MVC伟大的事情是它可以让你方便地访问构建自己的HTML。

使用MVC4你可以得到元素的ID和名称上与佣工前pression树的 HTML.NameFor HTML.IdFor

<选择名称=@ Html.NameFor(功能(模型)model.CityId)
        ID =@ Html.IdFor(功能(模型)model.CityId)
        类=location_city_input>
    @For每个城市在Model.Cities
        @<期权价值=@ city.Value
                 @(IF(city.Value = Model.CityId,选择,))
                 数据地理纬度=@ city.Lat
                 数据地理LNG =@ city.Lng
                 数据地理变焦=@ city.Zoom>
            @ city.Text
        < /选项>
    下一个
< /选择>

假设 Model.Cities 是揭露每个属性项的集合。然后,你应该所有设置。

如果你想重用性,可考虑将其用于任何一个编辑器模板是城市的可枚举

Is there anyway to have a SelectList prepopulated on ViewModel with data-attributes ?

I want to do

@Html.DropdownListFor(m=> m.CityId, Model.Cities);

so it generates code like :

<select id="City" class="location_city_input" name="City">
    <option data-geo-lat="-32.522779" data-geo-lng="-55.765835" data-geo-zoom="6" />
    <option data-geo-lat="-34.883611" data-geo-lng="-56.181944" data-geo-zoom="13" data-geo-name="Montevideo" data-child=".state1" value="1">Montevideo</option>               
    <option data-geo-lat="-34.816667" data-geo-lng="-55.95" data-geo-zoom="13" data-geo-name="Canelones, Ciudad de la Costa" data-child=".state41" value="41">Ciudad de la Costa</option>
</select>

解决方案

Here's the simple solution.

Not everything has to be written with extension method in .NET code. One of the great things about MVC is it gives you easy access to construct your own HTML.

With MVC4 you can get the id and name of the element on the expression tree with the helpers HTML.NameFor and HTML.IdFor

<select name="@Html.NameFor(Function(model) model.CityId)"
        id="@Html.IdFor(Function(model) model.CityId)"
        class="location_city_input">
    @For Each city In Model.Cities
        @<option value="@city.Value"
                 @(If(city.Value = Model.CityId, "selected", ""))
                 data-geo-lat="@city.Lat"
                 data-geo-lng="@city.Lng"
                 data-geo-zoom="@city.Zoom">
            @city.Text
        </option>
    Next
</select>

Assuming Model.Cities is a collection of items that expose each of those properties. Then you should be all set.

If you want reusability, consider making it an editor template for anything that is an Enumerable of Cities

这篇关于具有数据属性SelectListItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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