开发者

ASP.Net MVC Controller not being called

开发者 https://www.devze.com 2023-02-03 18:42 出处:网络
I have a MasterPage, in my Views\\Shared folder. I have an index.aspx in my Views\\Home folder. And I have a login.ascx (user control) in my Views\\User folder. The ascx file is added to my MasterPage

I have a MasterPage, in my Views\Shared folder. I have an index.aspx in my Views\Home folder. And I have a login.ascx (user control) in my Views\User folder. The ascx file is added to my MasterPage:

<% Html.RenderPartial(@"~\Views\User\login.ascx");%>

The code in the ascx is pointing the submit form to a UserController.cs in my Controllers folder.

Here's the ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.SiteUserLoginModel>" %>
<% using (Html.BeginForm("UserLogin", "User"))
   {%>
<table width="300">
    <tr>
        <td colspan="3" align="center">
            User Login
        </td>
    </tr>
    <tr>
        <td align="right">
            <%=Html.LabelFor(m => m.Username)%>
        </td>
        <td>
            <%=Html.TextBoxFor(m => m.Username)%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td align="right">
            <%=Html.LabelFor(m => m.Password)%>
        </td>
        <td>
            <%=Html.PasswordFor(m => m.Password)%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <%=Html.CheckBoxFor(m => m.RemeberMe)%><%=Html.LabelFor(m => m.Rem开发者_开发问答eberMe)%>
        </td>
    </tr>
    <tr>
        <td colspan="2">
        </td>
        <td colspan="3" align="right">
            <input id="Submit1" type="submit" runat="server" value="Login" />
        </td>
    </tr>
</table>
<%
   } // End Using.%>

If I ctrl+click the Controller name and the method, in the Html.BeginForm area, it takes me to my method that should be called when I click Sybmit.

The method that this should call, in my UserController, has this code defined:

        [HttpPost]
    public ActionResult UserLogin(SiteUserLoginModel model)
    {
        try
        {
            if (MembershipService.ValidateUser(model.Username, model.Password))
            {
                FormService.SignIn(model.Username, model.RemeberMe);
                PersonDto  user = SiteUserLoginModel.LoadLoggedInUserDetails(model.Username);
                Session.Add("current_firstname", user.Firstname);
                Session.Add("current_userid", user.PersonId);
            }
            return RedirectToAction("Index", "Home");
        }
        catch (ArgumentException e)
        {
            ViewData["ErrorMessage"] = e.Message;
            return RedirectToAction("Index", "Home");
        }
    }

However, when I click the submit button at runtime, this method is never called. The screen just refreshed. The HomeController does fire though, as the index screen is refreshed, I think. But the breakpoint in my method above never fires.

What am I doing wrong?


I would start removing the runat="server" attribute from your submit button...

<input id="Submit1" type="submit" value="Login" />


Your form in BeginForm isn't set to POST.

Try this: Html.BeginForm("UserLogin", "User", FormMethod.Post).


Guys, I found the issue, and it's an issue I had 2 weeks back, but totoally forgot. The master page had a Form tag! And the user control's Form tage was being hidden by the master page's one. I remove the Form tags from the master page, and it's resolved.

0

精彩评论

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