开发者

MVC C# Log In Masterpage

开发者 https://www.devze.com 2023-02-25 14:22 出处:网络
Hey, I\'m new to mvc .net. I have a masterpage which has a log in on it. Now i want to be able to log on from any page.

Hey, I'm new to mvc .net. I have a masterpage which has a log in on it. Now i want to be able to log on from any page. But i can't seem to get it to work. I have a AccountController with the method logon and a master page. This is the Login on the masterpage

 <% using (Html.BeginForm("LogOn", "Account"))
              { %>
           <%: Html.ValidationSummary(true, "Login foutief. probeer opnieuw.") %>
           <div id="login_panel">

                     <fieldset>

                        <div class="editor-label">
                             <%: Html.LabelFor(m => m.UserName) %>
                        </div>

                        <div class="editor-field">
                            <%: Html.TextBoxFor(m => m.UserName) %>
                            <%: Html.ValidationMessageFor(m => m.UserName) %>
                        </div>

                        <div class="editor-label">
                            <%: Html.LabelFor(m => m.Password) %>
                        </div>

                        <div class="editor-field">
                            <%: Html.PasswordFor(m => m.Password) %>
                            <%: Html.ValidationMessageFor(m => m.Password) %>
                        </div>

                       开发者_开发百科 <div class="editor-label">
                            <%: Html.CheckBoxFor(m => m.RememberMe) %>
                            <%: Html.LabelFor(m => m.RememberMe) %>
                        </div>

                        <p>
                        <input type="submit" value="Log On" />
                        </p>
                    </fieldset>

            </div>
            <% } %>

And this is the Controller

 [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (persoonRepos.aanmelden(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }
        return View(model);
    }

When i test it, it wont go in to the accountmodel LogOn method.

Someone who can help me?


Based on what you are saying, the only thing I can think of that is keeping that form from hitting the LogOn Controller Action is that the master page isn't aware of the LogOnModel and then can't pass it properly to the controller.

At the very top of your MasterPage you will have a line like this

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

If you have the form above embedded directly in your MasterPage, then it needs to be aware of the LogOnModel, so to make that happen make that top line look like this...

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<YourProjectName.Models.LogOnModel>" %>

Of Course replace "YourProjectName" with the name of your project...Then unless there is something else we're not seeing with your setup, that LogOn function should be hit when you submit the login.

0

精彩评论

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