views:

32

answers:

1

I'm creating asp.net mvc login page. This is simple. Same as others. Controller contains 2 method.

My problem is

I'm debugging First LogOn method. ReturnUrl has value. for example "Admin/Index". After debuggin Second LogOn method. But ReturnUrl is Null.

public ActionResult LogOn(string ReturnUrl) // First method
{
    return View();
}

[HttpPost]        
public ActionResult LogOn(string eUserName, string ePassword, Boolean rememberMe, string ReturnUrl) //Second Method
{
 ...
}

My View is here>>

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title>Login</title>    
</head>
<body>
<table style="width: 100%">
            <tr>
                <td align="center">
                    <div style="width: 800px;">
                        <%=Html.ValidationSummary() %>
                    </div>
                </td>
            </tr>
            <tr>
               <td align="center">
                 <% Html.RenderPartial("LoginControl"); %>
               </td>
            </tr>
    </table>
</body>
</html>

Logincontrol

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div style="text-align: left; width:150px;margin-top:20px;">
    <% using (Html.BeginForm("Logon", "Account", FormMethod.Post))
       { %>

    <div>
        Хэрэглэгчийн нэр:</div>
    <div>
        <%= Html.TextBox("eUserName") %>
    </div>
    <div style="margin-top:5px;">
        Нууц үг:</div>
    <div >
        <%= Html.Password("ePassword") %>
    </div>
    <div style="margin-top:5px;">
        <%= Html.CheckBox("rememberMe") %>
        <label class="inline" for="rememberMe">
            Намайг сана?</label>
    </div>
    <div style="text-align:center;margin-top:5px;">
        <input type="submit" value="Нэвтрэх" />
    </div>


    <%} %>
</div>

Why ReturnUrl returns null?

What happening?

EDIT

Thanks Tim Roberts

Last correct code is here>>

<% using (Html.BeginForm("Logon", "Account",new { ReturnUrl = HttpContext.Current.Request.QueryString["returnUrl"]} FormMethod.Post)){ %>
+2  A: 

Since you're implementing your own login mechanism, you'll need to ensure that the returnUrl parameter is passed along when the user clicks on your submit button. One way of doing this is to use an overload of the BeginForm method:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div style="text-align: left; width:150px;margin-top:20px;">
   <% using (Html.BeginForm("Logon", "Account", new { ReturnUrl = HttpContext.Current.Request.RawUrl }, FormMethod.Post))
      { %>
      ... as before
   <% } %>
</div>

Which should pass the URL along in the query string and then bind it to the action method's ReturnUrl parameter. Hope this helps.

Tim Roberts