设置选定的DropDownListFor项

时间:2013-11-05 18:07:15

标签: .net asp.net-mvc asp.net-mvc-3 html.dropdownlistfor

我无法使用下面的视图模型让DropDownListFor设置选定的StateCode

修改

根据以下给出的答案更改了代码。

模型

public partial class ZipCodeTerritory
{
    public string ChannelCode { get; set; }
    public string DrmTerrDesc { get; set; }
    public string IndDistrnId { get; set; }
    public string StateCode { get; set; }
    public string ZipCode { get; set; }
    public System.DateTime? DisplayEndDate { get; set; }
    public System.DateTime EndDate { get; set; }
    public System.DateTime EffectiveDate { get; set; }
    public string LastUpdateId { get; set; }
    public Nullable<System.DateTime> LastUpdateDate { get; set; }
    public int Id { get; set; }
}

查看模型

    public ZipCodeTerritory editPage { get; set; }

    //Constructor
    public ZipCodeEdit()
    {
        SetDropDowns();
    }

    //Drop down list objects
    private string[] states = new string[] { "AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "US", "UT", "VA", "VT", "WA", "WI", "WV", "WY" };
    public IEnumerable<SelectListItem> StateCodes { get; set; }
    public IEnumerable<SelectListItem> ChannelCodes { get; set; }

    //Set state code drop down list
    private void SetDropDowns()
    {
        AgentResources db = new AgentResources();

        //Set stat code drop down items
        StateCodes = StateCodes = states.Select(s => new SelectListItem { Value = s, Text = s });

        //Set channel code drop down list
        ChannelCodes = (from z in db.ZipCodeTerritory
                        select z.ChannelCode).Select(x => new SelectListItem
                        {
                            Text = x,
                            Value = x
                        }).Distinct().ToList();

        db.Dispose();
    }

控制器

    public ActionResult Edit(int id, string searchZip, bool searchActiveOnly, 
        string searchDate, string searchState, string searchTerritory, string searchChannel)
    {
        if (!Security.IsZipCodeViewOnly(User)) return RedirectToAction("Message", "Home", new { id = 1 });

        ZipCodeEdit pageItem = new ZipCodeEdit();

        pageItem.searchZip = searchZip;
        pageItem.searchActiveOnly = searchActiveOnly;
        pageItem.searchDate = searchDate;
        pageItem.searchState = searchState;
        pageItem.searchTerritory = searchTerritory;
        pageItem.searchChannelCode = searchChannel;
        pageItem.editPage = db.ZipCodeTerritory.Find(id);

        return View(pageItem);
    }

查看

这适用于编辑页面,因此视图需要加载ZipCodeTerritory对象的属性。使用此设置,DropDownListFor ChannelCode不仅会正确填充,而且会选择正确的值。 StateCode,但应尽量填充,但始终选择第一项AL。无法将StateCode对象的ZipCodeTerritory值作为默认选定项。

@model Monet.ViewModel.ZipCodeEdit


    <div class="editor-label">
        @Html.LabelFor(model => model.editPage.ChannelCode)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.editPage.StateCode, new SelectList(Model.StateCodes, "Value", "Text", Model.editPage.StateCode))
        @Html.ValidationMessageFor(model => model.editPage.ChannelCode)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.editPage.StateCode)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.editPage.StateCode, Model.StateCodes)
        @Html.ValidationMessageFor(model => model.editPage.StateCode)
    </div>

1 个答案:

答案 0 :(得分:3)

在您的视图中,将DropDownList更改为:

@Html.DropDownListFor(m => m.editPage.StateCode, Model.StateCodes)

此外,州私有财产不必是SelectList类型。它应该是一个字符串数组,如下所示:

private string[] states = new string[] { "AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "US", "UT", "VA", "VT", "WA", "WI", "WV", "WY" };        

然后,您将定义StateCodes属性,如下所示:

StateCodes = states.Select(s => new SelectListItem { Value = s, Text = s });

顺便说一句,View Models应该是简单的POCO类。不要在视图模型中初始化视图模型属性。在您的控制器中执行此操作。