【问题标题】:Not sure how to connect an HTML input button to a new ActionResult function不确定如何将 HTML 输入按钮连接到新的 ActionResult 函数
【发布时间】: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


    【解决方案1】:

    你必须使用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>
    

    现在单击提交按钮后,您的表单将发布在 Account 控制器的 Login 操作上,并且记住表单将仅使用 &lt;input type="submit"/&gt; 发布,按钮或链接将不工作。

    【讨论】:

      猜你喜欢
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多