0

我有一个登录表单,用户可以通过该表单登录我的应用程序。如果登录失败,则向客户端显示错误消息。

为此,我的控制器具有这两个功能:-

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

当用户输入错误的登录详细信息时,我在模型状态中添加了错误,然后使用正确的 msg 再次向客户显示视图。

但我的问题是它在浏览器上显示 html 代码,而不是在模型状态无效时呈现代码。

为什么会发生这些,我无法找到任何解决方案。

4

1 回答 1

2

您描述的问题是您视图中的问题,而不是您的控制器。如果您的 html 在属性中,那么您需要使用 @Html.Raw(Model.SomeProperty) 以使其不被编码。

于 2013-02-17T17:51:15.723 回答