【问题标题】:httppost to controller in mvc not working if value in model is blank or null如果模型中的值为空白或 null,则 httppost 到 mvc 中的控制器不起作用
【发布时间】:2012-08-09 07:55:01
【问题描述】:

我有以下代码可以发布到我的控制器中的 httppost 方法:

$QuickLoginSubmit.click(function (e) {
        e.preventDefault();
        var LoginModel = {
            'UserName': $QuickEmail.val() == "" ? null : $QuickEmail.val(),
            'Password': $QuickPassword.val() == "" ? null : $QuickPassword.val(),
            'RememberMe': $QuickRemember.val() == "on" ? true : false
        };
        $.ajax({
            url: '/Account/LogOnAjax',
            type: 'POST',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify(LoginModel),
            success: function (result) {
                if (result == true) {
                    window.location = "/Dashboard";
                } else {
                    $QuickLoginErrors.text(result);
                }
            }
        });
    });

问题是,如果他们没有在 $QuickEmail$QuickPassword 字段中输入任何内容,则 ajax 调用会返回以下错误:

The view 'LogOnAjax' or its master was not found or no view engine supports the searched locations. The following locations were searched:<br>~/Views/Account/LogOnAjax.aspx<br>~/Views/Account/LogOnAjax.ascx<br>~/Views/Shared/LogOnAjax.aspx<br>~/Views/Shared/LogOnAjax.ascx<br>~/Views/Account/LogOnAjax.cshtml<br>~/Views/Account/LogOnAjax.vbhtml<br>~/Views/Shared/LogOnAjax.cshtml<br>~/Views/Shared/LogOnAjax.vbhtml</i>

但是如果这两个字段都填写了,我的 ajax 方法调用就可以正常工作。这是我的httppost方法:

[HttpPost]
        public ActionResult LogOnAjax(LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    return Json(true);                  
                }
                else
                {
                    return Json("The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

【问题讨论】:

    标签: c# jquery asp.net ajax asp.net-mvc-3


    【解决方案1】:

    问题在于您的操作代码的最后一行。

    您实际上没有名为 LogOnAjax 的视图。所以,你应该使用

    return RedirectToAction(YOUR_LOGON_ACTION_NAME);
    

    或者让你的模型回到表单,你可以使用

    return View(YOUR_LOGON_VIEW_NAME, model);
    

    得到你想要的。

    更新:

    此外,如果您要在所有情况下都返回一个 json 对象,最好使用 JsonResult

    【讨论】:

      【解决方案2】:

      似乎您没有名为“LogOnAjax”的相关视图。由于您期望 JSON 响应,您可以做的就是像这样更改返回语句。

         // If we got this far, something failed, redisplay form
              return Json("Please enter valid username and password");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-12
        • 2011-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多