Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Please don't Postback the form. Instead of that Call the ajax call</strong> </p> <pre><code> &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;LMS.Data.User&gt;" %&gt; &lt;script src="&lt;%: Url.Content("~/Scripts/jquery-1.6.2.min.js") %&gt;"&gt;&lt;/script&gt; &lt;script src="&lt;%: Url.Content("~/Scripts/jquery.validate.min.js") %&gt;"&gt;&lt;/script&gt; &lt;script src="&lt;%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %&gt;"&gt;&lt;/script&gt; &lt;% using (Html.BeginForm()) &lt;%: Ajax.BeginForm("Create",new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "ManageUsersPartial", InsertionMode = InsertionMode.Replace }, htmlAttributes: new { data_target = "#tabs-users" }) { %&gt; &lt;%: Html.ValidationSummary(true, "Could not create new user.") %&gt; &lt;fieldset&gt; &lt;legend&gt;User&lt;/legend&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.UserName) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.EditorFor(model =&gt; model.UserName) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.UserName) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.FirstName) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.EditorFor(model =&gt; model.FirstName) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.FirstName) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.LastName) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.EditorFor(model =&gt; model.LastName) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.LastName) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.Password) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.EditorFor(model =&gt; model.Password) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Password) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.Email) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.EditorFor(model =&gt; model.Email) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Email) %&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%: Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; </code></pre> <p><strong>And then method should return the paratail view instead of Content Type</strong></p> <pre><code> public ActionResult Create(AccountUser user) { if (ModelState.IsValid) { try { user.Password = user.Password.Encrypt(); userManager.AddUser(user); } catch (DbEntityValidationException dbex) { foreach (var validationErrors in dbex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { ModelState.AddModelError(validationError.PropertyName, validationError.ErrorMessage); } } return PartialView("CreateUserPartial", user); } catch (DbUpdateException dbuex) { if (dbuex.InnerException != null) if (dbuex.InnerException.InnerException != null) if (dbuex.InnerException.InnerException.Message.Contains("Cannot insert duplicate key row in object")) return Content("User already exists!", "text/html"); else ModelState.AddModelError("", "An unknown error occured when creating the user."); return PartialView("CreateUserPartial", user); } catch (Exception) { ModelState.AddModelError("", "An unknown error occured when creating the user."); return PartialView("CreateUserPartial", user); } return PartialView("UsersPartial", userManager.GetUsers()); } return PartialView("UsersPartial", userManager.GetUsers()); } </code></pre>
 

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