编辑后:
我应该更好地说明我原来的问题:在下面的例子中,我使用两种形式:
using (Html.BeginForm....
在控制器中,我如何仅验证其中一个表单,而不是整个模型?这甚至可能吗?或者,我是否尝试以某种方式使用MVC?多年来,我一直是一名ASP.NET形式的人。还在学习MVC。
// END EDIT
我有一个带有表单的视图,我需要将其作为两部分(或两页)表单呈现。这两个部分都有一些必填字段。我能够模拟多页表单,但我遇到的问题是验证。对于每个帖子,它都会验证整个视图中的所有字段。如何让它仅验证当前可见的字段?
这就是我现在所拥有的(简化):
型号:
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...
}
}
}
答案 0 :(得分:3)
有很多方法可以做到:
我试着做最简单的
你的控制器:
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);
}
}
你的观点:LikelyToReturn:
@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>
}