views:

765

answers:

2

I have a partial view called LogOn where i basically copied the logon inputs into a control. I am using Html.RenderPartial to place the control in my Index.Html inside of an Ajax.BeginForm

<div id="login_ajaxtarget">
   <% using (Ajax.BeginForm("Logon", "Account", new AjaxOptions { UpdateTargetId = "login_ajaxtarget", HttpMethod = "Post" })) { %>

       <% Html.RenderPartial("LogOn"); %>

   <% } %>
        </div>

I am trying pass back the validation messages and have them display but i cant seem to get it to work. I am passing the model to the view but it doesnt seem to render the validation correctly.

My controller

public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
    {

        if (!ValidateLogOn(userName, password))
        {
            return PartialView("LogOn", ModelState);
            //return RedirectToAction("Index", "Home");
        }

        FormsAuth.SignIn(userName, rememberMe);
        if (!String.IsNullOrEmpty(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

My partial view

<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>


        <div>
            <fieldset>
                <legend>Account Information</legend>
                <p>
                    <label for="username">Username:</label>
                    <%= Html.TextBox("username") %>
                    <%= Html.ValidationMessage("username") %>
                </p>
                <p>
                    <label for="password">Password:</label>
                    <%= Html.Password("password") %>
                    <%= Html.ValidationMessage("password") %>
                </p>
                <p>
                    <%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
                </p>
                <p>
                    <input type="submit" value="Log On" />
                </p>
            </fieldset>
        </div>

I am trying to use the suggestions from this thread ( http://forums.asp.net/p/1398814/3023892.aspx#3023892 ) but im not sure if this is correct. All I really want is to be able to place the LogOn capability on the home page instead of having to navigate to a new page in order to use it. If there is an easier way to do this, Im all ears! thanks in advance.

A: 

I think somethings are required. You didn't check whether the inputs are empty or not and didn't add any error message to your model state.

// In your action add somethings like bellow.

if (String.IsNullOrEmpty(userName))
    this.ModelState.AddModelError("UserName", "The username is required.");

if (this.ModelState.IsValid)
{
    // Do login
}

return PartialView("Login");
Mehdi Golchin
Sorry, the code that i added was the code that i have changed so far. I have implemented the ValidateLogOn method (or more specifically, it has been implemented for me). I am merely trying to alter the out of the box website template provided when starting a new MVC project. My problem is that once the errors have been added to the model, they do not appear in the validationmessages on my page.
jimbo
A: 

Hey man really sorry. I haven't seen the default MVCApplication project template.

Check it out may solve your problem.

The control:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions() { UpdateTargetId = "UpdatePanel" }))
   { %>
<div id="UpdatePanel">
    <fieldset>
        <legend>Account Information</legend>
        <p>
            <label for="username">
                Username:</label>
            <%= Html.TextBox("username") %>
            <%= Html.ValidationMessage("username") %>
        </p>
        <p>
            <label for="password">
                Password:</label>
            <%= Html.Password("password") %>
            <%= Html.ValidationMessage("password") %>
        </p>
        <p>
            <%= Html.CheckBox("rememberMe") %>
            <label class="inline" for="rememberMe">
                Remember me?</label>
        </p>
        <p>
            <input type="submit" value="Log On" />
        </p>
    </fieldset>
</div>
<% } %>

The code:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{

    if (!ValidateLogOn(userName, password))
    {
        return PartialView("Login");
    }

    FormsAuth.SignIn(userName, rememberMe);
    if (!String.IsNullOrEmpty(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

It works for me. If the problem persists, check the response and request with Firebug.

Hope this helps

Mehdi Golchin