0

I have the Template MVC 4 change password view returning text instead of html:

Password View:

@model Heelp.ViewModels.LocalPasswordViewModel

<p>@ViewBag.StatusMessage</p>

@if(Model.HasLocalPassword)
{
    @Html.Partial(MVC.Account.Views._ChangePasswordPartial)
}
else
{ 
    @Html.Partial(MVC.Account.Views._SetPasswordPartial)
}

And the ChangePartial View:

@model Heelp.ViewModels.LocalPasswordViewModel

@if (!String.IsNullOrEmpty(Model.Result))
{
    <div class="result">
        @Model.Result
    </div>
}
@if (!Model.ChangePasswordSucceeded)
{
    using (Html.BeginForm(MVC.Account.Password())) 
    {
        @Html.AntiForgeryToken()

        @Html.LabelFor(m => m.OldPassword)
        @Html.PasswordFor(m => m.OldPassword)
        @Html.ValidationMessageFor(m => m.OldPassword)
        <br />
        @Html.LabelFor(m => m.NewPassword)
        @Html.PasswordFor(m => m.NewPassword)
        @Html.ValidationMessageFor(m => m.NewPassword)
        <br />
        @Html.LabelFor(m => m.ConfirmPassword)
        @Html.PasswordFor(m => m.ConfirmPassword)
        @Html.ValidationMessageFor(m => m.ConfirmPassword)
        <br />
        <input type="submit" value="@HeelpResources.ChangePasswordPartialSubmitButtonLabel" />
    }
}

And finally the controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult Password(LocalPasswordViewModel model)
    {
        // Has this information is lost in the roundtrip between the Controller and the Action, we need to get this information again
        model.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));

        if (model.HasLocalPassword)
        {
            if (ModelState.IsValid)
            {
                // It's a local account so update the new password
                model.ChangePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
            }

            // Send back to the View the results of the operation
            model.Result = model.ChangePasswordSucceeded ? HeelpResources.AccountManagePasswordChangeSucessMsg : HeelpResources.AccountManagePasswordChangeErrorMsg;

            return View(model);
        }
        else
        {
            try
            {
                // It's not a local account so create the account based on the external information and the password
                WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
                model.ChangePasswordSucceeded = true;
            }
            catch (Exception)
            {
                model.ChangePasswordSucceeded = false;
            }

            // User does not have a local password so remove any validation errors caused by a missing OldPassword field
            ModelState state = ModelState["OldPassword"];
            if (state != null)
            {
                state.Errors.Clear();
            }

            // Send back to the View the results of the operation
            model.Result = model.ChangePasswordSucceeded ? HeelpResources.AccountManagePasswordSetSucessMsg : HeelpResources.AccountManagePasswordSetErrorMsg;
        }

        return View(model);
    }

And when I submit the Password Change Form I get the page in text format:

< !DOCTYPE html >
< html xmlns="http://www.w3.org/1999/xhtml" >
< head >
  < meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  < title>izigo | tudo fica mais fácil</title>
  < meta name="viewport" content="width=960, user-scalable=yes"/>

//REST OF THE TEXT PAGE

Any ideas why is this happening?

Thanks.

Patrick
  • 2,995
  • 14
  • 64
  • 125

1 Answers1

2

Html.Partial returns a HTML-encoded string, which would explain the behaviour you're seeing.

If you want to render the view then perhaps try Html.RenderPartial instead.

Also, c.f. this Stack Overflow question

Community
  • 1
  • 1
nkvu
  • 5,551
  • 2
  • 16
  • 12
  • You either need to use `@Html.Raw(Html.Partial("MyPartial"))` or `@{ Html.RenderPartial("MyPartial"); }`. When you're not actually trying to get the partial as a string, then `RenderPartial` is preferred, since the default behavior is to dump the rendered output as-is to the page. – Chris Pratt Mar 25 '13 at 19:53
  • +1 another option is wrapping your `Partial` method with an HTML element like this `
    @Html.Partial(MVC.Account.Views._ChangePasswordPartial)
    `
    – Behnam Esmaili Mar 25 '13 at 19:54
  • Hi, thanks but it's still not working. The first time it presents the form, it's ok, but after submiting the form, the result it's the html page in Text (Chrome console says it's application/json?!). – Patrick Mar 27 '13 at 11:22
  • Hi Patrick, would you be able to post your updated code and also the full contents of the page you receive back? (I saw that you posted a snippet above but perhaps seeing the full page would help? – nkvu Mar 27 '13 at 14:04