简体   繁体   English

在Html.DropDownList Helper中设置默认值

[英]Setting Default Value in Html.DropDownList Helper

I have searched but have not been successful in getting a default value selected in a dropdownlist. 我已搜索但未成功获取在下拉列表中选择的默认值。

The following property does not reside in my MVC project, it is in my Core. 以下属性不在我的MVC项目中,它位于我的Core中。 Therefore, I didn't want to reference the System.Web.Mvc and used a Dictionary instead. 因此,我不想引用System.Web.Mvc而是使用Dictionary。

    [Display(Name = "Time Zone")]
    public int TimeZone { get; set; }
    public Dictionary<string, string> TimeZoneOptions
    {
        get
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("(GMT -10:00) Hawaii", "-10");
            d.Add("(GMT -9:00) Alaska", "-9");
            d.Add("(GMT -8:00) Pacific Time", "-8");
            d.Add("(GMT -7:00) Mountain Time", "-7");
            d.Add("(GMT -6:00) Central Time", "-6");
            d.Add("(GMT -5:00) Eastern Time", "-5");
            d.Add("Unknown", "0");
            return d;
        }
    }

I created a CreateViewModel in my MVC project so that I could convert the above Dictionary into a SelectList with a preselected Default value. 我在我的MVC项目中创建了一个CreateViewModel,以便我可以将上面的Dictionary转换为具有预选默认值的SelectList。

    public class CreateViewModel
{
    public SelectList GetUserTimeZoneList(string selectedValue)
    {
        return new SelectList(new BankUser().TimeZoneOptions, "Value", "Key", selectedValue);
    }
}

My view (notice the "-7" as the default value being passed) 我的观点 (注意“-7”作为传递的默认值)

    <div class="form-group">
        @Html.LabelFor(model => model.TimeZone, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.TimeZone, new CreateViewModel().GetUserTimeZoneList("-7"), new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.TimeZone)
        </div>
    </div>

The Results 结果

    <select class="form-control" data-val="true" data-val-number="The field Time Zone must be a number." data-val-required="The Time Zone field is required." id="TimeZone" name="TimeZone">
<option value="-10">(GMT -10:00) Hawaii</option>
<option value="-9">(GMT -9:00) Alaska</option>
<option value="-8">(GMT -8:00) Pacific Time</option>
<option value="-7">(GMT -7:00) Mountain Time</option>
<option value="-6">(GMT -6:00) Central Time</option>
<option value="-5">(GMT -5:00) Eastern Time</option>
<option selected="selected" value="0">Unknown</option>
</select>

As you can see, "Mountain Time" was not selected. 如您所见,未选择“山地时间”。 It always selects "Unknown". 它总是选择“未知”。 Any advice on what I am doing wrong? 关于我做错的任何建议?

It appears I overlooked an important aspect of the DropDownListFor helper. 看来我忽略了DropDownListFor帮助器的一个重要方面。 The first parameter of the control is actually the value that will be selected by default. 控件的第一个参数实际上是默认选择的值。

@Html.DropDownListFor(model => model.TimeZone, new SelectList(Model.TimeZoneOptions, "Value", "Key"), new { @class = "form-control" })

So when I pass the model to the view, it should be prepopulated with the correct default value like this: 因此,当我将模型传递给视图时,应该预先填充正确的默认值,如下所示:

    public ActionResult Create()
    {
        BankUser user = new BankUser();
        user.TimeZone = -7;
        return View(user);
    }

Therefore, I no longer need the original CreateViewModel class. 因此,我不再需要原始的CreateViewModel类。

When you use Html.DropDownListFor you are overriding the selected value. 当您使用Html.DropDownListFor您将覆盖所选值。 If you want the -7 as the default selected (such as a create form) populate the value of Model.TimeZone as -7 by default. 如果希望-7作为默认选择(例如创建表单),则默认情况下将Model.TimeZone的值填充为-7。 Optionally you can use the Html.DropDownList function. 您可以选择使用Html.DropDownList函数。

@Html.DropDownList("TimeZone", new CreateViewModel().GetUserTimeZoneList("-7"), new { @class = "form-control" })

Of course, then you will lose the value of TimeZone on an edit form. 当然,您将在编辑表单上丢失TimeZone的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM