user2500561
user2500561

Reputation: 133

Handler button click MVC4

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?

Upvotes: 0

Views: 1764

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

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

Upvotes: 1

Jonesopolis
Jonesopolis

Reputation: 25370

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.

Upvotes: 0

Related Questions