0
我有一个简单的模型,如:如何验证asp.net mvc 3中的多个模型?
public class AppointmentModel
{
private static List<SampleModel> _list;
public string ClientName {
get;
set;
}
[DataType(DataType.Date)]
public DateTime Date {
get;
set;
}
public bool TermsAccepted {
get;
set;
}
public List<SampleModel> SampleModelList {
get;
set;
}
public static List<SampleModel> GetSampleList() {
if (_list == null) {
_list = new List<SampleModel>(0);
_list.Add(new SampleModel {
Id = 1,
Name = "Test",
IsChecked = false
});
_list.Add(new SampleModel {
Id = 2,
Name = "Another test",
IsChecked = true
});
_list.Add(new SampleModel {
Id = 3,
Name = "All test",
IsChecked = false
});
}
return _list;
}
}
SampleModel
样子(创建它来模拟复选框上输入):
public class SampleModel
{
public int Id {
get;
set;
}
public string Name {
get;
set;
}
public bool IsChecked {
get;
set;
}
}
在我看来,我实现:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SimpleAppWithModelBinding.Models.AppointmentModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Page</h2>
<% using (Html.BeginForm(FormMethod.Post)) { %>
<%:Html.ValidationSummary()%>
<p>Name: <%: Html.EditorFor(model => model.ClientName)%></p>
<p>Date: <%: Html.EditorFor(model => model.Date)%></p>
<p><%: Html.EditorFor(model => model.TermsAccepted)%> Accept terms</p>
<%-- here I put the checkbox list using editor template --%>
<%: Html.Action("RenderPartialSample") %>
<input type="submit" value="Test" />
<%} %>
</asp:Content>
和控制器端HomeController
:
public ActionResult Page() {
return View();
}
// explicit validate model
[HttpPost]
public ActionResult Page(AppointmentModel model) {
// do some verification here
if (model.SampleModelList == null || (model.SampleModelList != null && model.SampleModelList.Count == 0)) {
ModelState.AddModelError("SampleModelList", "Please check something !");
}
if (ModelState.IsValid) {
//do something here
return View("Completed", model);
} else {
return View("Page", model);
}
}
public PartialViewResult RenderPartialSample() {
List<SampleModel> model = new List<SampleModel> {
new SampleModel{
Id = 1,
IsChecked = true,
Name = "Test"
},
new SampleModel{
Id = 2,
IsChecked = false,
Name = "Test1"
},
new SampleModel{
Id = 3,
IsChecked = false,
Name = "Test2"
}
};
AppointmentModel a = new AppointmentModel();
a.SampleModelList = model;
return PartialView("SamplePartialView", a.SampleModelList);
}
问题:
当我按下提交,该public ActionResult Page(AppointmentModel model)
给了我一个模型,他有SampleModelList
空,总是为空。我想从模型列表中检查输入,但由于局部视图可能不起作用。
如何验证我的情况下的两个模型?或者对我来说最好的办法是什么,也许我的做法不好。
请帮助:)
UPDATE:
SamplePartialView
包含:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<SimpleAppWithModelBinding.Models.SampleModel>>" %>
<%: Html.EditorForModel() %>
和模板:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SimpleAppWithModelBinding.Models.SampleModel>" %>
<%: Html.HiddenFor(x => x.Id) %>
<%: Html.CheckBoxFor(x => x.IsChecked, new {
value = Model.Id
})%>
<%: Html.LabelFor(x => x.IsChecked, Model.Name) %>
<br />
我更新了我的问题。感谢记得把所有的东西放在这里:) – 2013-02-27 12:32:56
是的,这正是我所想。你不尊重命名约定。查看生成的标记并确保遵守命名约定。 – 2013-02-27 12:36:24
好吧,我把'TemplateInfo',但HTTP_POST,我收到SampleModelList中的所有三个复选框(无论我检查了多少),并且'Name'参数为空。我验证了命名约定。 – 2013-02-27 12:43:28