RNO
RNO

Reputation: 223

.NET MVC Model Binding: nothing happen

Hello this is my first time with .NET MVC 3

I have a controller that is supposed to change password for the logged in User:

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();
}

The View is the following:

@model MVCApp.Models.ChangePasswordModel

@{
    ViewBag.Title = "Change User Password";
}

<h2>Change User Password</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Change Password</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.OldPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.OldPassword)
            @Html.ValidationMessageFor(model => model.OldPassword)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NewPassword)
            @Html.ValidationMessageFor(model => model.NewPassword)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConfirmPassword)
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

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?

Edit: Debugger does not find any error when I submit the form

Edit: (picture removed as not relevant)

I have changed the controller:

if (changePasswordSucceeded)
                {
                    return View("ChangePasswordSuccess");
                }
                else
                {
                    return View("ChangePasswordFailed");
                }

but it does not redirect me anywhere... I really don't understand what happens

EDIT: I think the model is empty

Upvotes: 0

Views: 584

Answers (5)

Karthik Chintala
Karthik Chintala

Reputation: 5545

You are not passing your model as overload to your View in your controllers action incase you have any model errors.

If(ModelState.IsValid)
{
     //your code
}
else
{
    return View(model);//You did not pass this 
}

Also in your view you never used ViewBag.Message to see whether your changePasswordSucceeded has failed. As you said the controller doesnot show anything it can fail.

You should use @ViewBag.Message in your view to check if there are any errors.

Upvotes: 1

RNO
RNO

Reputation: 223

I answer my own question as I found the problem now: the code was correct, the problem was in the Action link that was not sending a proper UserId to the Controller.

I found the problem by passing ViewBag.Message = UserId to the View, so I realised that it was empty. Fixed

Upvotes: 0

Anderson
Anderson

Reputation: 144

Remove the try. See if anything breaks.

Upvotes: 0

Nick Albrecht
Nick Albrecht

Reputation: 16918

If you suspect it may be Firefox doing it (that dialog popup), try using Chrome. While in Chrome navigate to the page you're trying to debug, and before you hit [Submit] hit F12 to bring up the Developer Tools, and switch to the Network Tab. After that open switch pack to the page and hit submit, make sure you see an entry in there where it tries to contact the server. If it does you can debug the problem in the action on the server, if nothing shows in the Network tab then it's likely something with the client side validation thinks it's not valid and is preventing it from submitting to the server.

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15851

mention your action and control in BeginForm.

@Html.BeginForm("action","controller", FormMethod.Post);

Upvotes: 1

Related Questions