我无法使用下面的视图模型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
forChannelCode
不仅被正确填充,而且选择了正确的值。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>