I am changing the form of creation to become a modal dialog and jquery. An unobtrusive check stops working and does not know how to fix it.
Index.cshtml has a link to launch a modal dialog.
<a href="#" id="createCustomer">Create</a> @section scripts{ <script type="text/javascript"> $('#createCustomer').on('click', function () { $.get('/Customer/Create', function (data) { $('#modalContainer').html(data); $('#myModal').modal({}); }); });
Create.cshtml is a partial view.
@model Demo.Web.Models.CustomerVm @using (Html.BeginForm("Create", "Customer", FormMethod.Post, new { @id="createForm" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Customer</h4> <hr /> @Html.ValidationSummary(true) <div class="form-group"> @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
There is an actionmethod on the controller that returns a partial view for the create form.
public ActionResult Create() { return PartialView("Create"); }
viewing modal
public class CustomerVm { [Required] public int Id { get; set; } [Required] public string Name { get; set; } }
before I changed it as a modal dialogue .. everything worked, but now I donβt know how to fix it. How can I validate with a dialog? Obviously, I do not want to rewrite the validation rules on the client script .. I want to make it work with the view model. Thanks.
asp.net-mvc data-annotations asp.net-mvc-viewmodel unobtrusive-validation
lawphotog
source share