Я пытаюсь заставить проверку формы работать в моем приложении MVC с помощью модального всплывающего окна Bootstrap. В настоящее время происходит, когда проверка формы не удается, это направляет меня к частичному представлению в полном браузере. Этого следовало ожидать, потому что я перенаправляю на действие частичного просмотра, но не могу заставить модал оставаться открытым.
Моя модель
public class CustomerModel
{
public int Id { get; set; }
[Required]
public string CustomerNo { get; set; }
[Required]
[DisplayName("Company Name")]
public string CompanyName { get; set; }
[Required]
public string AccountManager { get;set; }
}
Мой Index.cshtml
<a href="#" class="btn btn-link btn-float has-text" data-toggle="modal" data-target="#AddCustomerModal"><i class="icon-add text-primary"></i><span>Create Customer</span></a>
<div id="AddCustomerModal" class="modal fade">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-success">
<button type="button" class="close" data-dismiss="modal">×</button>
<h5 class="modal-title">Add Customer</h5>
</div>
<div class="form-horizontal">
<div class="modal-body">
@Html.Action("CreateModal")
</div>
</div>
</div>
</div>
</div>
Мое частичное представление (_CreateModal.cshtml) -
@model IHD.Core.Models.CustomerModel
@using (Html.BeginForm("CreateModal", "Customer", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="row">
<div class="col-md-12">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
<label class="col-form-label col-sm-3">Customer No.</label>
<div class="col-sm-4">
@Html.EditorFor(model => model.CustomerNo, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-3">Company Name</label>
<div class="col-sm-9">
@Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-3">Account Manager</label>
<div class="col-sm-9">
@Html.DropDownListFor(model => model.AccountManager, new SelectList(Model.UserOptions, "Key", "Value", Model.AccountManager), new { @name = "select", @class = "select-search" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Save" class="btn btn-success" />
</div>
</div>
</div>
</div>
}
Контроллер
[Authorize]
public ActionResult CreateModal()
{
CustomerModel customer = new CustomerModel();
return PartialView(customer);
}
[HttpPost]
public ActionResult CreateModal(CustomerModel model)
{
model.DateCreated = DateTime.Now;
model.CreatedBy = ((CustomPrincipal)HttpContext.User).Id;
model.LastUpdated = DateTime.Now;
model.LastUpdateBy = ((CustomPrincipal)HttpContext.User).Id;
if (ModelState.IsValid)
{
_customerService.Add(model);
return RedirectToAction("Details", "Customer", new { id = model.Id }).WithNotification("The customer " + model.CompanyName + " was created successfully.");
}
else
{
return PartialView(model);
}
}
Я убедился, что jquery.validate.min.js и jquery.validate.unobtrusive.min.js добавлены в Index.cshtml