views:

42

answers:

1

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

+1  A: 

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.

Darin Dimitrov
Worked very good :)Thank you but i was reading this book "Professional ASP.NET MVC 1.0" to learn and they put the View() instead of PartialView();Thanks again
Wahid Bitar