2013-02-16 58 views
0

我有一個用戶可以登錄我的應用程序的登錄表單。如果登錄失敗,則向客戶端顯示錯誤消息。模型狀態無效時在瀏覽器上顯示html代碼?

因爲我這個控制器有這個兩個功能: -

 [NopHttpsRequirement(SslRequirement.Yes)] 
    public ActionResult Login(bool? checkoutAsGuest) 
    { 
     if (_workContext.CurrentCustomer.IsRegistered()) 
     { 
      if (Request.QueryString["returnUrl"]!=null) 
      { 
       return Redirect(Convert.ToString(Request.QueryString["returnUrl"])); 
      } 

     } 

     var model = new LoginModel(); 
     model.UsernamesEnabled = _customerSettings.UsernamesEnabled; 
     model.CheckoutAsGuest = checkoutAsGuest.HasValue ? checkoutAsGuest.Value : false; 
     model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage; 
     return View(model); 
    } 

    [HttpPost] 
    [CaptchaValidator] 
    public ActionResult Login(LoginModel model, string returnUrl, bool captchaValid) 
    { 
     //validate CAPTCHA 
     if (_captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage && !captchaValid) 
     { 
      ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptcha")); 
     } 

     if (ModelState.IsValid) 
     { 
      if (_customerSettings.UsernamesEnabled && model.Username != null) 
      { 
       model.Username = model.Username.Trim(); 
      } 
      bool Active = true; 
      if (_customerRegistrationService.ValidateCustomer(_customerSettings.UsernamesEnabled ? model.Username : model.Email, model.Password, out Active)) 
      { 
       var customer = _customerSettings.UsernamesEnabled ? _customerService.GetCustomerByUsername(model.Username) : _customerService.GetCustomerByEmail(model.Email); 

       //migrate shopping cart 
       _shoppingCartService.MigrateShoppingCart(_workContext.CurrentCustomer, customer); 

       //sign in new customer 
       _authenticationService.SignIn(customer, model.RememberMe); 


       if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) 
       { 

        if (Request.QueryString["returnUrl"].ToString().Contains("search?q")) 
        { 

         string url = returnUrl; 
         if (Request.QueryString["select"] != null) 
          url = url + "&select=" + Request.QueryString["select"].ToString(); 

         if (Request.QueryString["startdate"] != null) 
          url = url + "&startdate=" + Request.QueryString["startdate"].ToString(); 


         if (Request.QueryString["enddate"] != null) 
          url = url + "&enddate=" + Request.QueryString["enddate"].ToString(); 

         if (Request.QueryString["s_loc"] != null) 
          url = url + "&s_loc=" + Request.QueryString["s_loc"].ToString(); 

         return Redirect(url); 
         // return Redirect(returnUrl + "&select=" + Request.QueryString["select"] == null ? "" : Request.QueryString["select"].ToString() + "&startdate=" + Request.QueryString["startdate"] == null ? "" : Request.QueryString["startdate"].ToString() + "&enddate=" + Request.QueryString["enddate"] == null ? "" : Request.QueryString["enddate"].ToString() + "&s_loc=" + Request.QueryString["s_loc"] == null ? "" : Request.QueryString["s_loc"].ToString()); 

        } 

        return Redirect(returnUrl); 

       } 
       else 
        return RedirectToRoute("HomePage"); 
      } 
      else 
      { 

       if (!Active) 
        ModelState.AddModelError("", _localizationService.GetResource("Account.Login.AccountNotactive")); 
       else 
        ModelState.AddModelError("", _localizationService.GetResource("Account.Login.WrongCredentials")); 
      } 
     } 

     //If we got this far, something failed, redisplay form 
     model.UsernamesEnabled = _customerSettings.UsernamesEnabled; 
     model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage; 
     return View(model); 
    } 

當用戶輸入錯誤登錄信息,我添加了一些錯誤的模型狀態,然後再次顯示視圖客戶適當味精。

但我的問題是它在瀏覽器上顯示html代碼,當模型狀態無效時不呈現代碼。

爲什麼這些發生,我無法找到任何解決方案。

+0

你的意思是代替** BOLD **顯示'BOLD''?你可以欣賞驗證摘要如何顯示在你的視圖中嗎? – dakait 2013-02-16 06:44:12

+0

你能澄清一點嗎? – 2013-02-16 06:47:11

回答

2

您所描述的問題在您的視圖中是一個問題,而不是您的控制器。如果你的html在一個屬性中,那麼你需要使用@ Html.Raw(Model.SomeProperty)以便它不被編碼。