[英]C# MVC Razor: Simulate Multi-Page Form w/ Validation
编辑后:
我本该更好地表达我的原始问题:在下面的示例中,我使用两种形式:
using (Html.BeginForm....
在控制器中,我将如何只验证其中一种形式,而不验证整个模型? 这有可能吗? 或者,我是否试图以非预期的方式使用MVC? 我多年来一直是ASP.NET表单专家。 仍在学习MVC。
//结束编辑
我有一个表单,需要以两部分(或两页)的形式呈现一个视图。 这两个部分都有一些必填字段。 我可以模拟多页表单,但是我遇到的麻烦是验证。 对于每个帖子,它都会验证整个视图上的所有字段。 我如何才能仅验证当前可见的字段?
这是我现在(简化)的内容:
模型:
public Boolean Page1Complete { get; set; }
public Boolean Page2Complete { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")]
public int? LikelyToReturn { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page2Question1Required")]
public int? RecomendToFriend { get; set; }
视图:
if (!Model.Page1Complete)
{
using (Html.BeginForm("PatientSatisfactionSurveyPage1", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
{
@for (var a = 0; a < 11; a++)
{
@a - @Html.RadioButtonFor(model => Model.LikelyToReturn, @a)
}
<input type="submit" id="page1-submit" name="page1-submit" value="Continue" class="btn green2">
}
}
else
// Page1 was submitted successfully. Display Page 2
{
using (Html.BeginForm("PatientSatisfactionSurveyPage2", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
{
@for (var a = 0; a < 11; a++)
{
@a - @Html.RadioButtonFor(model => Model.RecomendToFriend, @a)
}
<input type="submit" id="page2-submit" name="page2-submit" value="Complete" class="btn green2">
}
}
控制器:
[HttpPost]
public ActionResult PatientSatisfactionSurvey([Bind]PatientSatisfactionSurveyPage pss)
{
//Process and validate the first page
if (Request.Form["page1-submit"] != null)
{
if (ModelState.IsValid)
{
pss.Page1Complete = true;
// Page 1 Logic...
}
}
//Process and validate the first page
if (Request.Form["page2-submit"] != null)
{
if (ModelState.IsValid)
{
pss.Page2Complete = true;
// Page 2 Logic...
}
}
}
有很多方法可以做到:
我试图使最简单
您的控制器:
public class LikelyToReturnModel
{
[Required]
public int LikelyToReturn { get; set; }
}
public class RecomendToFriendModel
{
public int LikelyToReturn { get; set; }
[Required]
public int RecomendToFriend { get; set; }
}
public class PatientSatisfactionController : Controller
{
//
// GET: /PatientSatisfaction/
public ActionResult LikelyToReturn()
{
return View(new LikelyToReturnModel());
}
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult LikelyToReturn(LikelyToReturnModel model)
{
//validation example
if (model.LikelyToReturn == 0)
{
ModelState.AddModelError("", "Can't be zero!!!");
}
if (ModelState.IsValid)
{
return RedirectToAction("RecomendToFriend", new { LikelyToReturn = model.LikelyToReturn });
}
return View(model);
}
public ActionResult RecomendToFriend(int LikelyToReturn)
{
return View(new RecomendToFriendModel { LikelyToReturn = LikelyToReturn });
}
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult RecomendToFriend(RecomendToFriendModel model)
{
if (ModelState.IsValid)
{
//do something
}
return View(model);
}
}
您的观点可能返回:
@model MVCApp.Controllers.LikelyToReturnModel
<h2>LikelyToReturn</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
for (var a = 0; a < 11; a++)
{
@Html.RadioButtonFor(model => Model.LikelyToReturn, a) @a <br />
}
<button type="submit">Continue</button>
}
您的看法RecomendToFriend:
@model MVCApp.Controllers.RecomendToFriendModel
<h2>RecomendToFriend</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(_ => _.LikelyToReturn)
for (var a = 0; a < 11; a++)
{
@Html.RadioButtonFor(model => Model.RecomendToFriend, a) @a <br />
}
<button type="submit">Complete</button>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.