【发布时间】:2014-06-30 23:23:53
【问题描述】:
我是 ASP.NET MVC 4 的初学者,但我遇到了问题。基本上我有这个控制器:
public ViewResult Login()
{
return View(new LoginViewModel());
}
[HttpPost]
public ActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
if (authProvider.Authenticate(model.LoginUserName, model.LoginPassword))
{
return Redirect(Url.Action("Index", "Home"));
}
TempData["message"] = "Nome utente e/o password errati!!!";
return View();
}
return View();
}
这实现了一个简单的登录视图。我还创建了一个 ViewModel:
public class LoginViewModel
{
[Required(ErrorMessage = "Il nome utente è obbligatorio")]
[UIHint("TextBoxLogin")]
public string LoginUserName { get; set; }
[Required]
public string LoginPassword { get; set; }
}
最后我创建了 EditorTemplate:
@model string
<input name="@ViewData.TemplateInfo.HtmlFieldPrefix" id="@ViewData.TemplateInfo.HtmlFieldPrefix"data-validation="required" data-validation-error-msg="@ViewData["HelpMessage"]" value="@Model" />
到目前为止一切顺利。问题出在视图中。如果我把它放在视图中:
@using(Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.EditorForModel()
<p><input type="submit" value="Log in" /></p>
}
它就像一个魅力(但它把很多不想要的 html 放入页面),事实上,当我点击提交按钮时,它会转到控制器的 POST actionResult。如果我这样说:
@using (Html.BeginForm("Login","Account",FormMethod.Post))
{
<p class="username">
<label for="UserName">Nome utente</label>
@Html.EditorFor(m => m.LoginUserName, new { HelpMessage = "Il nome utente è obbligatorio!!!" });
</p>
<p class="password">
<label for="Password">Password</label>
@Html.EditorFor(m => m.LoginPassword)
</p>
<p>
<input type="submit" value="Log in" />
</p>
}
它不会出现在 post actionresult 上,但总是出现在 Get one 上。我想将这种类型的代码(最后一个)放入其中,我可以准确地设置 html,但我希望它继续 POST Actionresult,有人可以帮我理解为什么吗?
-----------------更新---------------- 这是生成的 HTML:
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="robots" content="noindex,nofollow" />
<link href="/static/css/login.css" rel="stylesheet" type="text/css" />
<link href="/static/css/jquery_ui.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]><link href="/static/css/lt_ie9.css" rel="stylesheet" type="text/css" /><![endif]-->
<script src="/static/js/jquery_1_10_2.js"></script>
<script src="/static/js/jquery_ui.js"></script>
<script src="/static/js/jquery_ui_function.js"></script>
</head>
<body>
<form>
<div id="top">
<div class="wrapped">
<div id="logo">TITLE</div>
</div>
</div>
<div id="content" class="user_student">
<div class="wrapped">
<div class="login_intro">
<h2>TEST</h2>
</div>
<div class="login_input">
<p id="error_messages"></p>
<h2>THIS ONE MAKES GET REQUEST</h2>
<form action="/Account/Login" method="post"> <p class="username"><label for="UserName">Nome utente</label>
<!--<input id="UserName" name="UserName" type="text"/>-->
<input name="LoginUserName" id="LoginUserName"data-validation="required" data-validation-error-msg="Il nome utente è obbligatorio!!!" />;
</p>
<p class="password"><label for="LoginPassword">Password</label>
<input class="text-box single-line" data-val="true" data-val-required="Il campo LoginPassword è obbligatorio." id="LoginPassword" name="LoginPassword" type="text" value="" />
</p>
<p><input type="submit" value="Log in" /></p>
</form> <p class="hidden">old</p>
</div>
<div class="login_footer">
<p>FOOTER</p>
</div>
</div>
</div>
<h2>THIS ONE MAKE POST REQUEST</h2>
<form action="/Account/Login?ReturnUrl=%2f" method="post"><div class="editor-label"><label for="LoginUserName">LoginUserName</label></div>
<div class="editor-field"><input name="LoginUserName" id="LoginUserName"data-validation="required" data-validation-error-msg="" /> <span class="field-validation-valid" data-valmsg-for="LoginUserName" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="LoginPassword">LoginPassword</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Il campo LoginPassword è obbligatorio." id="LoginPassword" name="LoginPassword" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="LoginPassword" data-valmsg-replace="true"></span></div>
<p><input type="submit" value="Log in" /></p>
</form><script src="/static/form-validator/jquery.form-validator.min.js"></script>
<script src="/static/js/jquery_form_validator_function.js"></script>
</form>
</body>
</html>
【问题讨论】:
-
“不需要的 html”是什么意思?我认为您的第二种观点没有任何问题。如果您将
[HttpGet]属性放在您的 GET 登录操作上,它仍然不起作用吗? -
1) 使用 Chrome 开发工具的网络选项卡来验证它是 POST 类型。 2) 你把
[HttpGet]放在Login()上了吗? MVC 将使用它找到的第一个满足条件的操作。如果你没有用[HttpGet]标记它,那么这意味着它适用于 GET 和 POST,因此将使用第一个操作。 -
我也很好奇您的
AccountController上是否有[Authorize]属性,如果有,您是否缺少操作方法上的[AllowAnonymous]属性? -
伙计们,感谢您的回答,但它仍然不起作用:-(这里有一些答案:@DavidG:不需要的 HTML 是从 @using(Html.BeginForm()) { @Html 生成的 html .EditorForModel() } 它放了很多div和自定义css类。我不在AccountController上做[Authorize], AccountController 检查登录选项。
-
@AaronLS 我放了 [HttpGet] 但它仍然不起作用。我查看了开发人员 Google 工具,它发送了一个获取而不是发布请求!!!但我设置使用 (Html.BeginForm("Login","Account",FormMethod.Post))...为什么?
标签: asp.net-mvc asp.net-mvc-4 mvc-editor-templates editortemplates