Note that there are some explanatory texts on larger screens.

plurals
  1. PORemote validation with additionalfields >> one more POST?
    text
    copied!<p>I have an ASP.NET MVC3 solution. I use remote validation for checking whether a username already exists.</p> <p>At a first stage, I user the following validation attribute:</p> <pre><code>[Required, Remote("UserNameAlreadyExists", "User", Error="Already exists!"] public string UserName { get; set; } </code></pre> <p>Here is the trace at runtime:</p> <p><img src="https://i.stack.imgur.com/Lgd9g.png" alt="enter image description here"></p> <p>Then I realised I need to pass additional field (userID) to validate thus I do:</p> <pre><code>[Required, Remote("UserNameAlreadyExists", "User", AdditionalFields="UserID", Error="Already exists!"] public string UserName { get; set; } </code></pre> <p>Here is the trace at runtime:</p> <p><img src="https://i.stack.imgur.com/Xmuet.png" alt="enter image description here"></p> <p>You'll see that there is one more POST ?!</p> <p>Why? Tt is a problem in my situation. This drives me crazy.</p> <p>Thanks for your help.</p> <hr> <p>UPDATE </p> <p>Here is the code of my Edit.cshtml</p> <pre><code>@using (Html.BeginForm()) { @Html.HiddenFor(m =&gt; m.UserID) @Html.LabelFor(m =&gt; m.UserName) @Html.TextBoxFor(m =&gt; m.UserName) ... &lt;input type="submit" value="Submit"&gt; } </code></pre> <p>Here is the javascript code</p> <pre><code> $('form', dialog).submit(function () { // Do not submit if the form does // not pass client side validation if (!$(this).valid()) { writeError("Validation failed!"); return false; } // On the line below: I retrieve the POST url + serialize all submitted data from my form $.post($(this).attr('action'), $(this).serialize(), function (data, status) { ... ... }).error(function (error, status, a, b) { alert('Error!'); }); // Unbind form submitting $('form', dialog).unbind(); return false; }); </code></pre> <p>When I debug my code (step by step) I can note some points:</p> <ul> <li>the form is submitted and intercepted by my jquery function</li> <li>the UserAlreadyExists is triggered (why?). In this function (on the second line) I have: var user = _requestServiceClient.GetUserFromUserName(userName); WHEN step on this line, I suddently step into post action of Edit. Strange!? Some step later, it step back in the function UserAlreadyExists. Still strange!?</li> </ul> <p>I don't know if the problem is in my code or if my version of jquery.validate.min.js is outdated and bugged or something else...</p> <p>I noticed that if I don't use any AdditionalFields, I don't have any problems!!</p> <p>I also noticed that if I trigger the UserAlreadyExists volontary (by focus in the field) I don't have any problems when submitting (posting)!! </p> <p><strong>So I have problems when the form Edit is showed and UserName field is not focused! Then I submit and problems arrived</strong></p> <p>Thanks.</p>
 

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