如何设置与Html.DropDownList的默认选项的值 [英] How do I set a value for the default option with Html.DropDownList

查看:1340
本文介绍了如何设置与Html.DropDownList的默认选项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP的MVC RC1。

I'm using ASP MVC RC1.

我使用的表格包含了我已经把这个code视图一个DropDownList。

A form I'm using contains a dropdownlist which I have put in a view with this code.

<%= Html.DropDownList("areaid", (SelectList)ViewData["AreaId"], "Select Area Id")%>

然而,渲染时,这就是我得到

However, when rendered, this is what I get

<select id="areaid" name="areaid">
   <option value="">Select Area Id</option>
   <option value="1">Home</option>
   ...
</select>

我想是选择区域ID选项,0值并将其标记默认为选中因此它与其他的价值观相一致,我可以验证一个区域是否已被选定为它是一个强制性的价值。 areaID表示是一个整数,所以当我点击当前的形式,而不在所有接触到的DropDownList,MVC抱怨说,不是一个整数,给了我一个绑定错误。

What I'd like is for the Select Area Id option to have a value of 0 and mark it as selected by default so it is consistent with the other values and I can validate whether or not an area has been chosen as it is a mandatory value. AreaId is an integer so when I currently click the form without touching the dropdownlist at all, MVC complains that "" is not an integer and gives me a binding error.

让我怎么设置的默认选项的值,然后让它在表格上选择?

SO how do I set a value for the default option and then make it selected on the form?

谢谢,丹

推荐答案

我觉得你有<击>三四个选项。首先,当你建立你的SelectList或枚举SelectItemList,prePEND与您的选择标签和默认值的选择。把它在顶部将其设置为默认,如果其他一些价值尚未在模型中选出。其次,你可以在视图中使用一个循环来创建选项构建选择(和期权)的手。再次,prepending默认选择,如果在模型中没有提供的。三,使用DropDownList的扩展,但使用JavaScript在页面加载修改后的第一个选项的值。

I think you have three four options. First when you are building your SelectList or enumerable of SelectItemList, prepend the selection with your option label and default value. Putting it at the top will make it the default if some other value isn't already chosen in the model. Second, you could build the select (and options) "by hand" in the view using a loop to create the options. Again, prepending your default selection if one isn't supplied in the model. Third, use the DropDownList extension, but modify the value of the first option using javascript after the page is loaded.

这似乎并不可能使用DropDownList的扩展值赋给一个optionLabel,因为它是硬codeD使用的String.Empty 。下面是 HTTP相关code段://www.$c$cplex.com/aspnet

It doesn't seem to be possible to use the DropDownList extension to assign a value to an optionLabel as it is hard-coded to use string.Empty. Below is the relevant code snippet from http://www.codeplex.com/aspnet.

    // Make optionLabel the first item that gets rendered.
    if (optionLabel != null) {
        listItemBuilder.AppendLine(ListItemToOption(new SelectListItem() { Text = optionLabel, Value = String.Empty, Selected = false }));
    }

修改:最后,最好的办法是让你的模型取空值并将其标记为使用需要RequiredAttribute标签。我会建议使用视图,具体型号而不是视图的实体模型。由于该值可为空,如果调回没有选择值空字符串将正常工作。其设置为需要的值会导致模型验证失败,所需要的价值相应的消息。这将允许您使用DROPDOWNLIST帮手原样。

EDIT: Finally, the best way is to have your model take a Nullable value and mark it as required using the RequiredAttribute. I would recommend using a view-specific model rather than an entity model for the view. Since the value is Nullable, the empty string will work fine if posted back without choosing a value. Setting it as a required value will cause the model validation to fail with an appropriate message that the value is required. This will allow you to use the DropdownList helper as is.

public AreaViewModel
{
    [Required]
    public int? AreaId { get; set; }

    public IEnumerable<SelectListItem> Areas { get; set; }
    ...
}

@Html.DropdownListFor( model => model.AreaId, Model.Areas, "Select Area Id" )

这篇关于如何设置与Html.DropDownList的默认选项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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