3
我得到了上面承受錯誤,而POST形式,我認爲受到錯誤的根本原因是「DropDownListFor」,其中多次SelectList兩次調用,如果是的,請提出解決方案?沒有爲此對象定義的無參數構造函數?
如果我從「x => x.Values」更改爲「x => x.Name」,那麼還會出現錯誤「There is no ViewData item of type'IEnumerable'has key'DDLView.Name'。 「
編輯模板
@model DropDownListViewModel
@Html.LabelFor(x=>x.Values, Model.Label)
@Html.DropDownListFor(x=>x.Values, Model.Values)
視圖模型
public class HomePageViewModel
{
public DropDownListViewModel DDLView { get; set; }
}
public class DropDownListViewModel
{
public string Label { get; set; }
public string Name { get; set; }
public SelectList Values { get; set; }
}
控制器
public ActionResult Index()
{
HomePageViewModel homePageViewModel = new HomePageViewModel();
homePageViewModel.DDLView = new DropDownListViewModel
{
Label = "drop label1",
Name = "DropDown1",
Values = new SelectList(
new[]
{
new {Value = "1", Text = "text 1"},
new {Value = "2", Text = "text 2"},
new {Value = "3", Text = "text 3"},
}, "Value", "Text", "2"
)
};
}
[HttpPost]
public ActionResult Index(HomePageViewModel model)
{
return View(model);
}
查看
@model Dynamic.ViewModels.HomePageViewModel
@using (Html.BeginForm())
{
@Html.EditorFor(x=>x.DDLView)
<input type="submit" value="OK" />
}
thnx的回覆,但wht應該寫在視圖和編輯器模板? – user584018 2012-07-12 00:25:06