0

单击提交按钮时,我被卡住了。单击提交按钮后,它不会返回到 HttpPost 操作。我正在共享我的代码。

控制器: 公共类 AccountController:控制器 {

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Account/LogOn
        public ActionResult UserLogin()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UserLogin(LogOn model)
        {
            if (ModelState.IsValid)
            {
                return View("Compltete", model);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }


    }

看法:

@model SanjiviniAnalytics.Models.LogOn

<h2>User Login</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("UserLogin", "Account", FormMethod.Post)) {

    <div>
        <fieldset>
            <legend>Sign In</legend>

            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </div>
            <p>
                <input type="submit" value="Log In" />
            </p>
        </fieldset>
    </div>

}

模型:

public class LogOn
{
    [Required]
    [Display(Name="User Name")]
    public string UserName { get; set; }

    [Required]
    [Display(Name="Password")]
    [DataType(DataType.Password)]
    public string Password { get; set;}
}

谁能帮我找出我哪里出错了?

4

3 回答 3

2

从表格中取出Controller.. MVC 不要求您在控制器名称中包含 Controller 部分:

@using (Html.BeginForm("UserLogin","Account"))

另外,尝试将 POST 指定为方法:

@using (Html.BeginForm("UserLogin", "Account", FormMethod.Post))
于 2012-08-16T12:05:49.783 回答
1

试试这个:

@using (Html.BeginForm("UserLogin","Account", FormMethod.Post))

(刚刚看到西蒙打败了我:))

于 2012-08-16T12:17:17.503 回答
0

正如西蒙所说,以下将起作用。HttpPost 操作将从 FormMethod.Post 方法中发生。

@using (Html.BeginForm("UserLogin", "Account", FormMethod.Post))
于 2012-08-16T13:01:18.273 回答