2013-03-25 67 views
0

我,而不是返回的HTML文本模板MVC 4更改密碼圖:Razor視圖渲染爲文本

密碼查看:

@model Heelp.ViewModels.LocalPasswordViewModel 

<p>@ViewBag.StatusMessage</p> 

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

而且ChangePartial查看:

@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" /> 
    } 
} 

而且最後控制器:

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

當我提交密碼更改表單我得到文本格式的網頁:文本頁

任何想法,爲什麼會出現這種情況的

< !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?

謝謝。

回答

2

Html.Partial返回一個HTML編碼的字符串,它可以解釋您所看到的行爲。

如果你想渲染視圖,那麼也許試試Html.RenderPartial

另外,c.f.這個堆棧溢出question

+0

您或者需要使用'@ Html.Raw(Html.Partial(「MyPartial」))''或'@ {Html.RenderPartial(「MyPartial」); }'。當你沒有真正想把偏好作爲一個字符串的時候,那麼'RenderPartial'是首選的,因爲默認的行爲是將渲染後的輸出原樣轉儲到頁面。 – 2013-03-25 19:53:26

+0

+1另一個選擇是用這種'

@Html.Partial(MVC.Account.Views._ChangePasswordPartial)
' – 2013-03-25 19:54:37

+0

這樣的HTML元素包裝你的'部分'方法嗨,謝謝,但它仍然無法正常工作。它第一次呈現表單,沒關係,但提交表單後,結果是Text中的HTML頁面(Chrome控制檯稱它是application/json ?!)。 – Patrick 2013-03-27 11:22:26