我是Razor MVC的新手,我无法弄清楚如何将HTML输入元素点击功能连接到ActionResult。这是我的代码:
我从项目中的Login.cshtml文件中获取此代码并将其放在Index.cshtml中:
@model S2GPortal.Models.LoginModel
.
.
.
<section id="featured">
<h2>Use a local account to log in.</h2>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
<p>
@*@Html.ActionLink("Register", "Register")*@ if you don't have an account.
</p>
}
</section>
这将在AccountController控制器上调用Login ActionResult方法。当它在登录视图中时。因为我把它放在索引视图中,所以不再调用Login方法,我无法弄清楚如何重新连接它以查看同一个控制器。我不确定MVC如何知道之前调用特定的Login ActionResult。这是控制器:
public class AccountController : BaseController
{
//
// GET: /Account/Login
[Inject]
public ISystemUserRepository SystemUserRepository { get; set; }
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (WebSecurity.IsAuthenticated)
{
string currentUser = WebSecurity.CurrentUserName;
int test = 1;
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
PortalSession.User = SystemUserRepository.GetByEmail(model.UserName);
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
所以,总而言之,一个视图如何知道调查ActionResult要调用的特定控制器,以及如何这样的一行:知道要调用哪个ActionResult?
谢谢!
答案 0 :(得分:2)
你必须使用Html.BeginForm
的重载:
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl }))
{
// your form elements
}
1)第一个参数是ActionName
2)第二个控制器名称
3)在您的情况下,第三种表格方法将在我们发布数据时发布
4),第四个是路线值
你的最终观点将是这样的:
@model S2GPortal.Models.LoginModel
<section id="featured">
<h2>Use a local account to log in.</h2>
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
}
</section>
现在点击“提交”按钮,您的表单将在登录 帐户的操作中发布控制器和记忆表单只会与<input type="submit"/>
,按钮或链接无效。