English 中文(简体)
MVC3 下调列表
原标题:MVC3 dropdownlistfor

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?

标签有效,但最好有一个下调菜单。

<强>溶解:

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

仅创建包含评级的 选择Listem 对象列表, 然后使用 Html. DropDownListFor 并存储在您的模型中的评级( 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 )
问题回答
@Html.DropDownListFor(model => model.RATE, new SelectList(Enumerable.Range(0, 11)))

这将对表单和表单中与表单和表单相关的数据进行此操作。

@Html.DropDownListFor(m => m.RATE, Model.RateSelectList, "<- Select Option ->")

模型。 选择列表将属于类型 IEVO 和 lt; 选择列表项目 & gt;, m. RATE 将是您的无效的整数( int? ) 属性。 第三个参数将是您默认的文本, 它将显示 m. RATE 是否无效 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签