处理器按钮单击MVC4



我是MVC4的新手,我有一些处理按钮点击的问题。

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");
    }
}
用户模式:

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

我有2个其他的用户名和密码文本框。当我点击

按钮时有两个问题
  1. "Login"动作在我的控制器是不火
  2. 信息发送到如下url:

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

我该如何修复它?

错误就在这里。你应该把HomeController改成Home。你必须传递不带后缀Controller:

的Controller Name
@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" />
}

和动作参数应该匹配输入元素名称:

行动:

[HttpPost]
public ActionResult Login(string ID, string Password) 
{
    //Some code here
    return View("Index");
}
更好的方法是使用强类型视图:
public class LoginModel
{    
    public string ID { get; set; }
    public string Password { get; set; }    
}

视图:

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

没有更多的代码去掉,看来你的问题是:

  1. 您的用户名和密码字段有id称为IDPassword,而您的控制器期望参数userpw,更改视图或控制器,使它们匹配。

  2. 你的URL看起来像你正在发送一个http GET请求,确保它是POST

  3. 正如Ehsan所说,不要在你的HomeController上使用Controller后缀来引用它。

相关内容

  • 没有找到相关文章

最新更新