Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Client Side</h1> <p>Using the <code>jQuery.validate</code> library should be pretty simple to set up.</p> <p>Specify the following settings in your <code>Web.config</code> file:</p> <pre><code>&lt;appSettings&gt; &lt;add key="ClientValidationEnabled" value="true"/&gt; &lt;add key="UnobtrusiveJavaScriptEnabled" value="true"/&gt; &lt;/appSettings&gt; </code></pre> <p>When you build up your view, you would define things like this:</p> <pre><code>@Html.LabelFor(Model =&gt; Model.EditPostViewModel.Title, true) @Html.TextBoxFor(Model =&gt; Model.EditPostViewModel.Title, new { @class = "tb1", @Style = "width:400px;" }) @Html.ValidationMessageFor(Model =&gt; Model.EditPostViewModel.Title) </code></pre> <p><strong><em>NOTE:</strong> These need to be defined within a form element</em></p> <p>Then you would need to include the following libraries:</p> <pre><code>&lt;script src='@Url.Content("~/Scripts/jquery.validate.js")' type='text/javascript'&gt;&lt;/script&gt; &lt;script src='@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")' type='text/javascript'&gt;&lt;/script&gt; </code></pre> <p>This should be able to set you up for client side validation</p> <h2>Resources</h2> <ul> <li><a href="http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvccustomvalidation_topic5.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvccustomvalidation_topic5.aspx</a></li> </ul> <h1>Server Side</h1> <p><strong>NOTE:</strong> This is only for additional server side validation on top of <code>jQuery.validation</code> library</p> <p>Perhaps something like this could help:</p> <pre><code>[ValidateAjax] public JsonResult Edit(EditPostViewModel data) { //Save data return Json(new { Success = true } ); } </code></pre> <p>Where <code>ValidateAjax</code> is an attribute defined as:</p> <pre><code>public class ValidateAjaxAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.HttpContext.Request.IsAjaxRequest()) return; var modelState = filterContext.Controller.ViewData.ModelState; if (!modelState.IsValid) { var errorModel = from x in modelState.Keys where modelState[x].Errors.Count &gt; 0 select new { key = x, errors = modelState[x].Errors. Select(y =&gt; y.ErrorMessage). ToArray() }; filterContext.Result = new JsonResult() { Data = errorModel }; filterContext.HttpContext.Response.StatusCode = (int) HttpStatusCode.BadRequest; } } } </code></pre> <p>What this does is return a JSON object specifying all of your model errors.</p> <p>Example response would be</p> <pre><code>[{ "key":"Name", "errors":["The Name field is required."] }, { "key":"Description", "errors":["The Description field is required."] }] </code></pre> <p>This would be returned to your error handling callback of the <code>$.ajax</code> call</p> <p>You can loop through the returned data to set the error messages as needed based on the Keys returned (I think something like <code>$('input[name="' + err.key + '"]')</code> would find your input element</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload