Note that there are some explanatory texts on larger screens.

plurals
  1. POForm posting in MVC4 using AJAX - partialview and ModelState.IsValid and modalbox
    primarykey
    data
    text
    <p>I'm using ASP.NET MVC4 to create an application and I'm facing an issue regarding AJAX and partial views.</p> <p>Right now, I have a view rendering :</p> <ul> <li>a partial view (called <code>_createUser</code>) which is a modalbox containing a Form to create a user in my application (rendered via <code>@{Html.RenderPartial("_createUser");}</code>)</li> <li>a partial action (called <code>ListUsers</code>) which is an action that simply renders users currently registered in my application (rendered via <code>@{Html.RenderAction("users");}</code> in an html table</li> </ul> <p>When the user clicks a button, the modal box contained in <code>_createUser</code> is displayed via jquery so the user can type informations about a new user. When he clicks on a button in the modal box, the form is submitted via AJAX to an action in my controller which checks <code>ModelState.IsValid</code>.</p> <p>And this is where I'm stuck because I don't know what is the correct way of handling success/error with partial view using jquery because if the model is correct and the registration is correct, I want to refresh only the list of users. Is the model is not valid, I want to refresh the modal box with errors raised by the <code>ModelState</code>.</p> <p>My action is :</p> <pre><code>[HttpPost] [ValidateAntiForgeryToken] public ActionResult Register( RegisterModel model ) { if( ModelState.IsValid ) { try { WebSecurity.CreateUserAndAccount( model.UserName, model.Password ); WebSecurity.Login( model.UserName, model.Password ); ViewBag.CreateUserSucceded = true; return RedirectToAction( "ListUsers" ); } catch (MembershipCreateUserException e) { ModelState.AddModelError( "", ErrorCodeToString( e.StatusCode ) ); } } return PartialView( "_createUser", model ); } </code></pre> <p>My jquery is pretty simple too :</p> <pre><code>function SubmitCreateUserForm() { $.ajax({ type: "POST", url: '@Url.Action("Register")', data: $("#createUserForm").serialize(), success: function (result, status, jqXHR) { ???? } }); } </code></pre> <p>I think the correct way should be to use JSON and, instead of returning and <code>ActionResult</code>, I should return some JSON containing for a status field and the data related to my users list or validation errors and using the status field to know which part of my main view must be updated. By I really don't know how to mix JSON result and a razor view. Any clue?</p> <p><strong>Updated (I can't answer my own question right now or I have to wait 8 hours).</strong></p> <p>Ok, I ended up using what I found in this link <a href="http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/" rel="nofollow">http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/</a></p> <p>It's pretty straightforward and does the job really well!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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