我是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个问题信息已发送到网址,如:
http://localhost:31648/?ID=administrator&Password=123
我该如何解决?
答案 0 :(得分:1)
这是错误。您应该将HomeController
更改为Home
。您必须在没有后缀控制器的情况下传递控制器名称:
@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" />
}
在控制器中:
public class HomeController : Controller
{
[HttpPost]
public ActionResult Login(LoginModel model)
{
//Some code here
return View("Index");
}
}
答案 1 :(得分:0)
没有更多的代码可以解决,看来你的问题是:
您的用户名和密码字段的ID为ID
和Password
,而您的控制器需要参数user
和pw
,请更改视图或控制器使它们匹配。
您的网址似乎在发送http GET
请求,请确保其为POST
正如Ehsan所说,请勿使用Controller
上的HomeController
后缀在您的视图中引用它。