模型A有两个属性,模型B强类型,模型类为IEnumerable
期望实现以下目标: 接收表单数据 - >验证这两个模型 - >转移收藏。
技术难度:
Form data:
Model A:
property one, property two data [Pass]
Model B:
Rec#1: Title, Firstname, Lastname [Available in the collection]
Rec#2: Title, Firstname, lastname [Not available in the collection]
我使用模型B向控制器提交两行数据(参见图片)。集合仅包含第一行数据。 模型b是强类型类,无法验证集合或不知道如何收集数据和验证。怎么做?
模型A
public class TestOneModel
{
[Required]
public string PropertyOne { get; set; }
[Required]
public string PropertyTwo { get; set; }
}
模型B
public class PassengerModel
{
public string Title { get; set; }
[Required(ErrorMessage = "Please enter first name")]
[StringLength(20, ErrorMessage = "First Name not be exceed 20 char")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter last name")]
[StringLength(20, ErrorMessage = "LastName not be exceed 20 char")]
public string LastName { get; set; }
}
查看模型
public class IndexViewModel
{
public TestOneModel TestOne { get; set; }
public PassengerModel NewPassenger { get; set; }
public List<PassengerModel> Passengers { get; set; }
}
查看
@model MultipleModelsDemo.ViewModels.IndexViewModel
@using MultipleModelsDemo.ViewModels.vmConstants
@using MultipleModelsDemo.Helpers
@{
Html.Raw(TempData["passengerOrdering"]);
}
<div>
@using (Html.BeginForm("MyEditActionOne", "Checkout"))
{
<div>
<h4>Send Model One</h4>
<fieldset>
@Html.ValidationSummary(true, "ValidationSummary")
<ol>
<li>
@Html.LabelFor(m => m.TestOne.PropertyOne)
@Html.TextBoxFor(m => m.TestOne.PropertyOne)
@Html.ValidationMessageFor(m => m.TestOne.PropertyOne)
</li>
<li>
@Html.LabelFor(m => m.TestOne.PropertyTwo)
@Html.TextBoxFor(m => m.TestOne.PropertyTwo)
@Html.ValidationMessageFor(m => m.TestOne.PropertyTwo)
</li>
</ol>
</fieldset>
<div style="margin:3%;">
@*@Html.Partial("~/Views/Checkout/Index.cshtml", Model.Passengers)*@
@foreach (var item in Model.Passengers)
{
//Html.RenderPartial("EditorRow", item);
<div class="editorRow">
//@using (Html.BeginCollectionItem("PassengerModel"))
//{
@Html.Raw(TempData["passengerOrdering"])
<li>
@Html.LabelFor(m => m.NewPassenger.Title, "Title")
@Html.DropDownListFor(m => m.NewPassenger.Title, vmConstants.EnqTypes)
</li>
<li>
@Html.LabelFor(m => m.NewPassenger.FirstName, "First Name")
@Html.TextBoxFor(m => m.NewPassenger.FirstName, new { size = "25" })
@Html.ValidationMessageFor(m => m.NewPassenger.FirstName)
</li>
<li>
@Html.LabelFor(m => m.NewPassenger.LastName, "Last Name")
@Html.TextBoxFor(m => m.NewPassenger.LastName, new { size = "25" })
@Html.ValidationMessageFor(m => m.NewPassenger.LastName)
</li>
//}
</div>
}
</div>
<input class="btn" type="submit" value="Send" />
</div>
}
</div>
控制器
[HttpPost]
public ActionResult MyEditActionOne(IndexViewModel model)
{
if (ModelState.IsValid)
{
return View("Index", model);
}
model.Passengers = _repository.GetPassengers();
return View("Index", model);
}
答案 0 :(得分:0)
您确定包含该脚本:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"));
并加入:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<appSettings>
对于验证,您可以抓住这样的错误:
ModelState.AddModelError("keyName","Message");
并表明:
@Html.ValidationMessage("keyName")