Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET MVC Model Binding: nothing happen
    text
    copied!<p>Hello this is my first time with .NET MVC 3</p> <p>I have a controller that is supposed to change password for the logged in User:</p> <pre><code>public ActionResult ChangeUserPassword(string userId) { ChangePasswordModel model = new ChangePasswordModel() { Id = userId }; return View(model); } [HttpPost] public ActionResult ChangeUserPassword(ChangePasswordModel model) { if (ModelState.IsValid) { // ChangePassword will throw an exception rather // than return false in certain failure scenarios. bool changePasswordSucceeded = true; try { changePasswordSucceeded = userDetailsService.ChangeUserPassword(model.Id, model.OldPassword, model.NewPassword); } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded) { return View("ChangePasswordSuccess"); } else { ViewBag.Message = "The current password is incorrect or the new password is invalid."; ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } } // If we got this far, something failed, redisplay form return View(); } public ActionResult ChangePasswordSuccess() { return View(); } </code></pre> <p>The View is the following: </p> <pre><code>@model MVCApp.Models.ChangePasswordModel @{ ViewBag.Title = "Change User Password"; } &lt;h2&gt;Change User Password&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Change Password&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.Id) &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.OldPassword) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.OldPassword) @Html.ValidationMessageFor(model =&gt; model.OldPassword) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.NewPassword) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.NewPassword) @Html.ValidationMessageFor(model =&gt; model.NewPassword) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.ConfirmPassword) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.ConfirmPassword) @Html.ValidationMessageFor(model =&gt; model.ConfirmPassword) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>When I click on the button 'Save', nothing happen. Can you either suggest me if I am doing something wrong or how to catch and display the error?</p> <p>Edit: Debugger does not find any error when I submit the form</p> <p>Edit: (picture removed as not relevant)</p> <p>I have changed the controller:</p> <pre><code>if (changePasswordSucceeded) { return View("ChangePasswordSuccess"); } else { return View("ChangePasswordFailed"); } </code></pre> <p>but it does not redirect me anywhere... I really don't understand what happens</p> <p><strong>EDIT</strong>: I think the model is empty</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