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