0

我无法使用下面的视图模型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对象的属性。使用此设置,DropDownListForforChannelCode不仅被正确填充,而且选择了正确的值。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>
4

1 回答 1

3

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

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

此外,states 私有属性不必是 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 类。不要在视图模型中初始化视图模型属性。在您的控制器中执行此操作。

于 2013-11-05T18:43:41.300 回答