【发布时间】:2014-06-16 18:21:34
【问题描述】:
我是 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 方法。当它在登录视图中时。由于我已将它放在 Index 视图中,因此不再调用 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?
谢谢!
【问题讨论】:
标签: c# jquery asp.net-mvc asp.net-mvc-4 razor