当我点击我的'继续'按钮我的表单生成正在验证的字段的新视图,并显示丢失的内容。如何在同一部分视图上显示验证。
我的控制器:
namespace LoanApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
// This is how we prepopulate the list of loan types. Doing it here b/c one day this will be from a list from a service.
var vm = new Step1ViewModel();
vm.AllAvailableLoanTypes = new List<string>();
vm.AllAvailableLoanTypes.Add("Auto Refinance");
vm.AllAvailableLoanTypes.Add("Auto Purchase");
vm.AllAvailableLoanTypes.Add("Auto PreApproval");
vm.AllAvailableLoanTypes.Add("Signature Loan");
vm.AllAvailableLoanTypes.Add("Credit Card");
vm.AllAvailableLoanTypes.Add("Home Equity");
vm.AllAvailableLoanTypes.Add("10 Year 1st Mortgage");
return View(vm);
}
[HttpPost]
public ActionResult SelectLoanType(Step1ViewModel model)
{
// For this example I slapped the type in Session, you could post it through in each subsequent viewmodel and just hide it on the form in a Html.Hidden()
Session["LoanType"] = model.LoanType;
return PartialView("PrimaryApplicantPartial");
}
[HttpPost]
public ActionResult PrimaryApplicant(PrimaryApplicantViewModel model)
{
// Make sure session didnt get eaten.
var loanType = "";
if (Session["LoanType"] != null)
{
loanType = Session["LoanType"].ToString();
}
// Here we decide which view to show next. in the frotn end you may need to handle what to change labels to in the wizard maybe via JQ/JS
switch (loanType)
{
case "Auto Refinance":
if (ModelState.IsValid)
{
return PartialView("AutoRefinance");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
case "Auto Purchase":
if (ModelState.IsValid)
{
return PartialView("AutoPurchase");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
case "Auto PreApproval":
if (ModelState.IsValid)
{
return PartialView("AutoPreApproval");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
case "Signature Loan":
if (ModelState.IsValid)
{
return PartialView("SignatureLoan");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
case "Credit Card":
if (ModelState.IsValid)
{
return PartialView("CreditCard");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
case "Home Equity":
if (ModelState.IsValid)
{
return PartialView("HomeEquity");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
case "10 Year 1st Mortgage":
if (ModelState.IsValid)
{
return PartialView("TenYearFirstMortgage");
}
else
{
ModelState.AddModelError("", "Information Incomplete");
return PartialView("PrimaryApplicantPartial");
}
default:
return PartialView("PrimaryApplicantPartial");
}
}
[HttpPost]
public ActionResult AutoRefinance(AutoRefinanceViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("AutoRefinanceFinal");
}
[HttpPost]
public ActionResult AutoPurchase(AutoPurchaseViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("NextView");
}
[HttpPost]
public ActionResult AutoPreApproval(AutoPreApprovalViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("NextView");
}
[HttpPost]
public ActionResult SignatureLoan(SignatureLoanViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("NextView");
}
[HttpPost]
public ActionResult CreditCard(CreditCardViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("NextView");
}
[HttpPost]
public ActionResult HomeEquity(HomeEquityViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("NextView");
}
[HttpPost]
public ActionResult TenYearFirstMortgage(TenYearFirstMortgageViewModel model)
{
//do stuff here
// This will fail for now with a lovely 500
return PartialView("NextView");
}
}
}
您将看到上面的验证码。这是我的部分观点:
@using Newtonsoft.Json
@model LoanApp.Models.PrimaryApplicantViewModel
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.maskedinput.js"></script>
<script>
$("input[name='JointOwner']").change(function () {
$("#JointApplicantInfo").toggle();
});
jQuery(function ($) {
$("#dob").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
$("#ssn").mask("999-99-9999");
$("#zip").mask("99999");
});
</script>
@using (Ajax.BeginForm("PrimaryApplicant", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "step3", OnSuccess = "showStep3" }))
{
<h4>Primary Applicant Information</h4>
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<hr/>
<div class="form-group">
@Html.LabelFor(m => m.FirstName, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", placeholder = "First Name" })
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.MiddleName, new { @class = "form-control", placeholder = "Middle Name" })
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.LastName, new { @class = "form-control", placeholder = "Last Name" })
</div>
@Html.ValidationMessageFor(m => m.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.SSN, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.SSN, new { @class = "form-control", placeholder = "Social Security Number", id = "ssn" })
@Html.ValidationMessageFor(m => m.SSN)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.DOB, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.DOB, new { @class = "form-control", placeholder = "Date of Birth", id = "dob" })
@Html.ValidationMessageFor(m => m.DOB)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", type = "email", placeholder = "Email Address" })
</div>
@Html.ValidationMessageFor(m => m.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.PhoneNumber, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control", placeholder = "Phone Number", id = "phone" })
</div>
@Html.ValidationMessageFor(m => m.PhoneNumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Address1, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.Address1, new { @class = "form-control", placeholder = "Address 1" })
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.Address2, new { @class = "form-control", placeholder = "Address 2" })
</div>
@Html.ValidationMessageFor(m => m.Address1)
</div>
</div>
<div class="form-group">
<div class="col-md-3"></div>
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.City, new { @class = "form-control", placeholder = "City" })
@Html.ValidationMessageFor(m => m.City)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.State, new { @class = "form-control", placeholder = "State" })
@Html.ValidationMessageFor(m => m.State)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.Zip, new { @class = "form-control", placeholder = "Zip Code", id = "zip" })
@Html.ValidationMessageFor(m => m.Zip)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.HousingPayment, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.HousingPayment, new { @class = "form-control", placeholder = "Housing Payment" })
@Html.ValidationMessageFor(m => m.HousingPayment)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.HousingType, new { @class = "form-control", placeholder = "Housing Type" })
@Html.ValidationMessageFor(m => m.HousingType)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmploymentType, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.EmploymentType, new { @class = "form-control", placeholder = "Employment Type" })
@Html.ValidationMessageFor(m => m.EmploymentType)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmployerName, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.EmployerName, new { @class = "form-control", placeholder = "Employer Name" })
@Html.ValidationMessageFor(m => m.EmployerName)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmploymentTitle, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.EmploymentTitle, new { @class = "form-control", placeholder = "Title" })
@Html.ValidationMessageFor(m => m.EmploymentTitle)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmploymentStartDate, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.EmploymentStartDate, new { @class = "form-control", type = "date", placeholder = "Employment Start Date" })
@Html.ValidationMessageFor(m => m.EmploymentStartDate)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.MonthlyIncome, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.MonthlyIncome, new { @class = "form-control", placeholder = "Monthly Income" })
@Html.ValidationMessageFor(m => m.MonthlyIncome)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AdditionalIncome, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.AdditionalIncome, new { @class = "form-control", placeholder = "Additional Income" })
@Html.ValidationMessageFor(m => m.AdditionalIncome)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AdditionalIncomeSource, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.AdditionalIncomeSource, new { @class = "form-control", placeholder = "Additional Income Source" })
@Html.ValidationMessageFor(m => m.AdditionalIncomeSource)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AdditionalIncomeAmt, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.AdditionalIncomeAmt, new { @class = "form-control", placeholder = "Additional Income Amount" })
@Html.ValidationMessageFor(m => m.AdditionalIncomeAmt)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointOwner, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
<label>@Html.RadioButtonFor(m => m.JointOwner, new { @class = "form-control", value = "0" }, new { @checked = "" }) No</label>
<label>@Html.RadioButtonFor(m => m.JointOwner, new { @class = "form-control", value = "1" }) Yes</label>
</div>
</div>
</div>
//Joint Applicant
<div id="JointApplicantInfo">
<h4>Joint Applicant Information</h4>
<div class="form-group">
@Html.LabelFor(m => m.JointFirstName, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointFirstName, new { @class = "form-control", placeholder = "First Name" })
@Html.ValidationMessageFor(m => m.JointFirstName)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointMiddleName, new { @class = "form-control", placeholder = "Middle Name" })
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointLastName, new { @class = "form-control", placeholder = "Last Name" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointSsn, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointSsn, new { @class = "form-control", placeholder = "Social Security Number", id = "ssm" })
@Html.ValidationMessageFor(m => m.JointSsn)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointDob, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointDob, new { @class = "form-control", placeholder = "Date of Birth", id = "dob" })
@Html.ValidationMessageFor(m => m.JointDob)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointEmail, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointEmail, new { @class = "form-control", type = "email", placeholder = "Email Address" })
</div>
@Html.ValidationMessageFor(m => m.JointEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointPhoneNumber, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointPhoneNumber, new { @class = "form-control", placeholder = "Phone Number", id = "phone" })
</div>
@Html.ValidationMessageFor(m => m.JointPhoneNumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointAddress1, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointAddress1, new { @class = "form-control", placeholder = "Address 1" })
@Html.ValidationMessageFor(m => m.JointAddress1)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointAddress2, new { @class = "form-control", placeholder = "Address 2" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-3"></div>
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointCity, new { @class = "form-control", placeholder = "City" })
@Html.ValidationMessageFor(m => m.JointCity)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointState, new { @class = "form-control", placeholder = "State" })
@Html.ValidationMessageFor(m => m.JointState)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointZip, new { @class = "form-control", placeholder = "Zip Code" })
@Html.ValidationMessageFor(m => m.JointZip)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointHousingPayment, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointHousingPayment, new { @class = "form-control", placeholder = "Housing Payment" })
@Html.ValidationMessageFor(m => m.JointHousingPayment)
</div>
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointHousingType, new { @class = "form-control", placeholder = "Housing Type" })
@Html.ValidationMessageFor(m => m.JointHousingType)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointEmploymentType, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointEmploymentType, new { @class = "form-control", placeholder = "Employment Type" })
@Html.ValidationMessageFor(m => m.JointEmploymentType)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointEmployerName, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointEmployerName, new { @class = "form-control", placeholder = "Employer Name" })
@Html.ValidationMessageFor(m => m.JointEmployerName)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointEmploymentTitle, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointEmploymentTitle, new { @class = "form-control", placeholder = "Title" })
@Html.ValidationMessageFor(m => m.JointEmploymentTitle)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointEmploymentStartDate, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointEmploymentStartDate, new { @class = "form-control", type = "date", placeholder = "Employment Start Date" })
@Html.ValidationMessageFor(m => m.JointEmploymentStartDate)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointMonthlyIncome, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointMonthlyIncome, new { @class = "form-control", placeholder = "Monthly Income" })
@Html.ValidationMessageFor(m => m.JointMonthlyIncome)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointAdditionalIncome, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointAdditionalIncome, new { @class = "form-control", placeholder = "Additional Income" })
@Html.ValidationMessageFor(m => m.JointAdditionalIncome)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointAdditionalIncomeSource, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointAdditionalIncomeSource, new { @class = "form-control", placeholder = "Additional Income Source" })
@Html.ValidationMessageFor(m => m.JointAdditionalIncomeSource)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.JointAdditionalIncomeAmt, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
<div class="col-md-4">
@Html.TextBoxFor(m => m.JointAdditionalIncomeAmt, new { @class = "form-control", placeholder = "Additional Income Amount" })
@Html.ValidationMessageFor(m => m.JointAdditionalIncomeAmt)
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9 ">
<div class="col-md-4">
<input type="submit" value="Continue" class="btn btn-default" />
</div>
</div>
</div>
}
答案 0 :(得分:0)
如果我理解你要做的是你想要返回部分视图,如果它失败(模型状态),你需要重新定向或传递另一个,如果modelstate有效。
如果你想做上面的场景,你可以组合使用jquery和json
使用jquery(序列化表单)将表单提交给post方法,如果要重定向到其他位置,则返回json结果
如果我错了,请纠正我。答案 1 :(得分:0)
好的,你走的正确,只需尝试这几个步骤。
在您的控制器上设置一个条件,您的模型状态是否有效
在你的控制器上你需要返回带有参数的路径url的json对象。
在控制器上
返回Json(new {redirectToAction = Url.Action(&#34; ActionResult&#34;,&#34; Controller&#34;,new {Parameters = parameters})});
你应该是jquery函数 对于默认情况,bind add将您的url作为字符串传递给jquery函数。
函数SubmitForm(routUrl){
var serializedForm = $("# YourFormName ").serialize();;
$.ajax({
url: routUrl,
cache: false,
type: "POST",
data: serializedForm,
success: function (data) {
if (data.redirectToAction) {
window.location.href = data.redirectToAction;
}
else {
$("#YourFormName").html(data);
}
}
});
}
如果您需要更多详细信息,请告诉我,希望这会对您有所帮助。