我有一个页面有两个下拉列表,根据这两个列表的选择,我想用textarea填充提交按钮按下的一些数据。
我在调试时看到的行为是页面呈现,我做出选择并按提交。 DataAccess返回正确的结果并返回View,但有一个例外“没有类型'IEnumerable'的ViewData项具有'People'键。
我可以看到我可以重新设置下拉列表,但感觉我正在接近这个错误。在MVC 3中有另一种方法可以做这种动作吗?
public ActionResult Test()
{
//People for dropdownlist 1
var db = peopleRepository.People;
var query = db.Select(c => new {c.Id, c.Name});
ViewBag.People = new SelectList(query.AsEnumerable(), "Id", "Name");
//Elements for dropdownlist 2
var list = new Dictionary<string, string> {{"1", "Name"}, {"2", "Address"}, {"3", "Zip"}};
ViewBag.Elements = new SelectList(list, "Key", "Value");
return View();
}
// This part is what I'm confused about.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test(string people, string elements)
{
if (ModelState.IsValid)
{
// Output from persistent storage query
var da = new DatabaseAccess(people, elements);
ViewBag.Results = da.Execute();
}
return View();
}
查看:
@using (Html.BeginForm("Test", "Home", FormMethod.Post))
{ @ Html.DropDownList(“People”,(SelectList)ViewBag.People,“ - 选择一个 - ”) @ Html.DropDownList(“Elements”,(SelectList)ViewBag.Elements,“ - 选择一个 - ”) @ Html.TextArea(“Results”,(string)ViewBag.Results,10,120,“”) }
答案 0 :(得分:2)
以下是我如何快速构建它:
型号:
public class People
{
public int Id { get; set; }
public string Name { get; set; }
}
ViewModel(视图需要的所有内容):
public class TestViewModel
{
public int SelectedPeopleId { get; set; }
public string SelectedElementId { get; set; }
public SelectList People { get; set; }
public SelectList Elements { get; set; }
public String Results { get; set; }
}
Controller(使用Index作为默认Action,为可以调整的视图模型创建一个init函数)更合适:
public class HomeController : Controller
{
private static TestViewModel InitTestVM()
{
//People for dropdownlist 1
var db = new List<People>();//peopleRepository.People;
db.Add(new People { Id = 1, Name = "Name 1" });
db.Add(new People { Id = 2, Name = "Name 2" });
var query = db.Select(c => new { c.Id, c.Name });
//Elements for dropdownlist 2
var list = new Dictionary<string, string> { { "1", "Name" }, { "2", "Address" }, { "3", "Zip" } };
TestViewModel testVM = new TestViewModel
{
People = new SelectList(query.AsEnumerable(), "Id", "Name"),
Elements = new SelectList(list, "Key", "Value")
};
return testVM;
}
public ActionResult Index()
{
return View(InitTestVM());
}
// This part is what I'm confused about.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(TestViewModel testVM)
{
var vm = InitTestVM();
if (ModelState.IsValid && testVM != null)
{
ModelState.Clear();
// Output from persistent storage query
//var da = new DatabaseAccess(people, elements);
vm.Results = "sfdfsdfsdfsdfsdfsdfsdfsdf";//da.Execute();
vm.SelectedElementId = testVM.SelectedElementId;
vm.SelectedPeopleId = testVM.SelectedPeopleId;
return View(vm);
}
return View(vm);
}
}
最后是视图:
@model ViewModels.TestViewModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.DropDownListFor(m => m.SelectedPeopleId, Model.People, "--Select One--")
@Html.DropDownListFor(m => m.SelectedElementId, Model.Elements, "--Select One--")
@Html.TextAreaFor(m => m.Results, 10, 120, "")
<input type="submit" value="Test" />
}