0

I'm newbie in MVC4 and i have some problem with handle button click.

Index.cshtml:

<form id="Form1" runat="server">
<div class="logo">
    <br />
    <img alt="" src="../../Images/logo.png" />
    <br />
    @Html.Partial("~/Views/Home/PartialView/Login.cshtml");
</div>
</form>

Login.cshtml:

@model AP.MVC4.Models.User

<fieldset>
    <legend><b>
        @Html.ViewBag.login_text
    </b></legend>
    <table>
        <tr>
            <td>
                <label>
                    @Html.ViewBag.user_name
                </label>
            </td>
            <td>
                @Html.TextBoxFor(Model=>Model.ID, new { style = "width:60%;" })
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    @Html.ViewBag.password
                </label>
            </td>
            <td>
                @Html.PasswordFor(Model=>Model.Password, new { style = "width:60%;" })
            </td>
        </tr>
        <tr>
            <td colspan="2">
                @using (Html.BeginForm("Login", "HomeController", FormMethod.Post))
                {
                    <input id="btnLog" type="submit" value="@Html.ViewBag.login" />
                }
            </td>
        </tr>
    </table>
</fieldset>

HomeController.cs:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.login = Resources.LocalString.login;
        ViewBag.login_text = Resources.LocalString.login_text;
        ViewBag.user_name = Resources.LocalString.user_name;
        //....Some other code
        return View();
    }
    [HttpPost]
    public ActionResult Login(string user, string pw) {
        MD5 md5Hash = MD5.Create();
        string Pw_hash = GetMd5Hash(md5Hash, pw);
        DataTable dt = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, "GetLoginDetail"
            , new SqlParameter("@UserName", user.Trim())
            , new SqlParameter("@Password", Pw_hash.ToLower())).Tables[0];
        //...Some other code
        return View("Index");
    }
}

User model:

public class User
{
    private const string RequireMessage = "Bắt buộc";
    [Required(ErrorMessage = RequireMessage)] 
    [Key]
    public string ID { get; set; }
    [Required(ErrorMessage = RequireMessage)] 
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [Required(ErrorMessage = RequireMessage)] 
    public string Name { get; set; }
    public string BirthDay { get; set; }
    public string CMND { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
}

I have 2 other textbox for username and password. There are 2 problems when I click the button

  1. "Login" action in my controller was not fire
  2. Information was sent to url like:

    http://localhost:31648/?ID=administrator&Password=123
    

How can I fix it?

3
  • Post you code that generates the inputs for username and password (the names of the controls do not appear to match the parameters or your post action method) Commented Jul 28, 2014 at 2:14
  • I have just update all the code, please take a look Commented Jul 28, 2014 at 2:33
  • The inputs generated by @Html.TextBoxFor and @Html.PasswordFor need to be inside the form element (see code for the View in Ehsan's answer) Commented Jul 28, 2014 at 2:41

2 Answers 2

1

Here is the mistake. You should change HomeController to Home. You have to pass Controller Name without the postfix Controller:

@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
    <input type="text" name="ID"/>
    <input type="password" name="Password"/>
    <input id="btnLog" type="submit" value="@Html.ViewBag.login" />
}

And action parameters should match the input element name:

Action:

[HttpPost]
public ActionResult Login(string ID, string Password) 
{
    //Some code here
    return View("Index");
}

More better way is to use strongly typed view:

public class LoginModel
{    
    public string ID { get; set; }
    public string Password { get; set; }    
}

View:

@model LoginModel 

@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
    @Html.TextBoxFor(x=>x.ID)
    @Html.PasswordFor(x=>x.Password)
    <input id="btnLog" type="submit" value="@Html.ViewBag.login" />
}

And in Controller:

public class HomeController : Controller
{    
    [HttpPost]
    public ActionResult Login(LoginModel model) 
    {
        //Some code here
        return View("Index");
    }    
}
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work. And my class is HomeController, why only use "Home" ?
we have to pass only controller name without controller postfix
I found the problem, it because in Index.cshtml i have a <form> tag surround everything.
you have form inside form?
you should have posted your complete view, you only posted the mvc Helper form
0

without more code to go off of, it appears your problems are:

  1. Your username and password fields have id's called ID and Password, while your controller expects parameters user and pw, change either the view or the controller so they match up.

  2. Your URL looks like you're sending an http GET request, make sure it's a POST

  3. As Ehsan said, don't use the Controller suffix on your HomeController to reference it in your view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.