I created a partial view in an MVC 3 application. This view has a strongly typed model as follows:
public class ProductViewModel { [Required, Display(Name = "Product price")] public decimal? ProductPrice { get; set; } ... }
In my action method, I call a PartialView method like this
PartialView("ProductViewModel", products[0]);
But on the page I do not see the markup for the verification logic, but nothing happens if there are errors on the page. If I use this partial view as an editor template, it works. Any help is appreciated.
Edit: To be more specific, I have an HTML form and I want to add markup to it using an ajax update (if the user clicks a button, I want to add a new markup to this form). If I enable these controls statically, I mean, if I create them when the page loads, the check is done, but if I add the controls to this form with ajax call, no check mark will be inserted for these controls . My partial view is as follows:
@Html.LabelFor(x => x.ProductPrice) @Html.TextBoxFor(x => x.ProductPrice) @Html.ValidationMessageFor(x => x.ProductPrice)
My form is as follows:
@using (Html.BeginForm()) { <div id="div_Products"> @Html.EditorFor(x => x) </div> <input type="submit" value="Compare" /> }
The above code works well, validation works. On the server side, I call an action method that looks like this:
[HttpPost] public ActionResult InsertProduct() { var newProductVM = new ProductViewModel{ ProductPrice = 789 }; return PartialView("~/Views/Nutrition/EditorTemplates/ProductViewModel.cshtml", newProductVM); }
I realized that the MVC engine only inserts this check markup if it detects that the controls are inside the form control. When I try to update my form control using an ajax call, MVC cannot know that they will be placed inside the form element and why it does not emit any validation logic for them. I guess.
unobtrusive-javascript validation asp.net-mvc-3 data-annotations
Zoliqa
source share