Asp.net mvc 3 MVC3部分视图方法未触发
我有一个局部视图(登录,使用用户名、密码和提交按钮),局部视图用于我的布局(materpage) 因此,在我的布局页面上,我有:Asp.net mvc 3 MVC3部分视图方法未触发,asp.net-mvc-3,layout,partial-views,Asp.net Mvc 3,Layout,Partial Views,我有一个局部视图(登录,使用用户名、密码和提交按钮),局部视图用于我的布局(materpage) 因此,在我的布局页面上,我有: <div style="text-align: right"> @Html.Partial("_LoginPartial") </div> 我在所有方法上都有断点,但它们从未被命中。按“提交”按钮只需将我的URL更改为: http://localhost:8741/?Username=myusername&Password=m
<div style="text-align: right">
@Html.Partial("_LoginPartial")
</div>
我在所有方法上都有断点,但它们从未被命中。按“提交”按钮只需将我的URL更改为:
http://localhost:8741/?Username=myusername&Password=mypassword
有人能发现我所犯的错误吗?由于Html.BeginForm默认为发出GET请求,因此您正在从视图中发出GET请求。但是,您的操作只接受POST请求 您可以使用(Html.BeginForm(“索引”、“帐户”))更改
。
使用(Html.BeginForm(“index”,“Account”,FormMethod.Post))尝试了这一点,但仍然未能达到断点。我还以为表单post默认为post呢?@Craig,如果表单将您重定向到
http://localhost:8741/?Username=myusername&Password=mypassword
,这表示HTTP GET请求,因为浏览器将GET表单提交转换为查询参数。为了协助调试,您可以尝试检查浏览器发出的网络请求。您可以使用浏览器附带的开发人员工具或第三方工具(如Fiddler)来实现这一点。感谢Stephen-Fiddler的帮助。我发现这归结于我的_布局有for标记,这就扼杀了我登录框上的表单标记。固定的。谢谢
@using GalleryPresentation.Models
@model LoginModel
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
@using (Html.BeginForm("index", "Account"))
{
<table>
<tr>
<td>@Html.LabelFor(m => m.Username)</td>
<td>@Html.TextBoxFor(m => m.Username)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.Password)</td>
<td>@Html.PasswordFor(m => m.Password) kjkj</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login"/></td>
</tr>
<tr>
<td colspan="2">@Html.ValidationSummary()</td>
</tr>
</table>
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if(ModelState.IsValid)
{
var g = new GallaryImage();
var user = g.LoginUser(loginModel.Username, loginModel.Password);
if(user != null)
{
FormsAuthentication.SetAuthCookie(user.username, false);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid Username/Password");
}
return View(loginModel);
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
http://localhost:8741/?Username=myusername&Password=mypassword