【问题标题】:In asp.net mvc3 on button submit, not causing action to be performed在按钮提交的asp.net mvc3中,不会导致执行操作
【发布时间】:2012-08-16 12:03:34
【问题描述】:

我在单击提交按钮时卡住了。单击提交按钮后,它不会返回到 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;}
}

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

【问题讨论】:

  • 那么当按钮被点击时会发生什么?是不是又调用Get方法了?
  • 是的。它每次都会调用get方法

标签: asp.net-mvc-3


【解决方案1】:

Controller 去掉。MVC 不要求您在控制器名称中包含 Controller 部分:

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

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

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

【讨论】:

  • 我做了先生,但它没有解决我的问题。
  • @using (Html.BeginForm("UserLogin", "Account", FormMethod.Post))
【解决方案2】:

试试这个:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      相关资源
      最近更新 更多