MVC3 dropdownlistfor [英] MVC3 dropdownlistfor

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

问题描述

我想作一个下拉列表,编号为0-10。因此,用户可以评价的东西。
此刻,我有一个标签:@ Html.LabelFor(型号=> model.RATE)
我怎么能修改此code,我将有一个下拉框?而且该下拉框的值将被存储在model.RATE?

该标签的工作,但它会好得多有一个下拉菜单。

解决方案:

  @ Html.DropDownListFor(型号=> model.RATE,Enumerable.Range(0,11)。选择(X =>新建SelectListItem的{text = x.ToString() }));


解决方案

只要创建 SelectListItem 包含收视率,然后用 Html.DropDownListFor 与存储在模型中的等级( Model.RATE )。

  @ {
    VAR收视率=新的List< SelectListItem>();
    对于(VAR I = 0;我LT&= 10;我++){
        days.Add(新SelectListItem的{text = i.ToString(),值= i.ToString(),选择= Model.RATE == I});
    }
}
@ Html.DropDownListFor(X => x.RATE,评级)

I would like to make a dropdown list, with the numbers 0-10. So users can rate something. At the moment, I have a label: @Html.LabelFor(model=> model.RATE) How can I modify this code that I will have a dropdown box? And that the value of the dropdown box will be stored in model.RATE?

The label is working, but it would be much better to have a dropdown menu.

SOLUTION:

@Html.DropDownListFor(model => model.RATE, Enumerable.Range(0,11).Select( x => new SelectListItem { Text = x.ToString() }));

解决方案

Just create a list of SelectListItem objects that contain the ratings, and then use Html.DropDownListFor with the rating stored in your model (Model.RATE).

@{
    var ratings = new List<SelectListItem>();
    for( var i = 0; i <= 10; i++ ) {
        days.Add( new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = Model.RATE == i } );
    }
}
@Html.DropDownListFor( x => x.RATE, ratings )

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

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