开发者

Why always fires OnFailure when return View() to Ajax Form?

I'm trying to make a log-in log-off with Ajax supported.

I made some logic in my controller to sign the user in and then return simple partial containing welcome message and log-Off ActionLink my Action method looks like this :

public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(model.UserName, model.Password))
        {
            FormsService.SignIn(model.UserName, model.RememberMe);
            if (Request.IsAjaxRequest())
            {
             //HERE IS THE PROBLEM :(
        开发者_高级运维        return View("LogedInForm", model);
            }
            else
            {
                if (!String.IsNullOrEmpty(returnUrl))
                    return Redirect(returnUrl);
                else
                    return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            if (Request.IsAjaxRequest())
            {
                return Content("There were an error !");
            }
        }
    }
    return View(model);
}

and I'm trying to return this simple partial :

 Welcome <b><%= Html.Encode(Model.UserName)%></b>!
 <%= Html.ActionLink("Log Off", "LogOff", "Account") %>

and of-course the two partial are strongly-typed to LogOnModel.

But if i returned View("PartialName") i always get OnFailure with status code 500. While if i returned Content("My Message") everything is going right.

so please tell me why i always get this "StatusCode = 500" ??. where is the big mistake ??.


Try like this:

if (Request.IsAjaxRequest())
{
    return PartialView("LogedInForm", model);
}

Make sure that you have a strongly typed LogedInForm.ascx in the same view folder as the controller name.

If this doesn't fix the error try using FireBug in order to see the exact error message which the action returns to the AJAX request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜