3

Click the MVC4 Manager button

 2 years ago
source link: https://www.codesd.com/item/click-the-mvc4-manager-button.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Click the MVC4 Manager button

advertisements

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?


here is the mistake, change HomeController to Home, we 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:

pubic class HomeController : Controller
{

  [HttpPost]
  public ActionResult Login(LoginModel model)
  {
        //Some code here
        return View("Index");
  }

}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK