ASP.NET MVC + Modeling and Partial View

I have a partial view called LogOn, where I basically copied the input to the control input. I use Html.RenderPartial to place the control in my Index.Html inside 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 to pass verification messages and show them, but I cannot get it to work. I am passing the model to the view, but it does not seem to do the correct check.

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>

(http://forums.asp.net/p/1398814/3023892.aspx#3023892), , . , , - LogOn , , . , Im ! .

+3
2

, - . , , .

// 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");
0

, . MVCApplication .

, .

:

<%@ 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>
<% } %>

:

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

. , Firebug.

,

0

Source: https://habr.com/ru/post/1719915/


All Articles