【问题标题】:Submit button not working! MVC提交按钮不起作用! MVC
【发布时间】:2016-02-14 19:01:35
【问题描述】:
@model CommonLayer.ORDER
@{
ViewBag.Title = "Update";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h4>Update Order Status</h4>
<br />

@if (Model.OrderStatus != "Shipped")
{
using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.UserId)

        <div class="form-group">
            @Html.LabelFor(model => model.OrderId, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select class="form-control" name="OrderStatus">
                    <option value="Pending" id="pending">Pending</option>
                    <option value="Shipped" id="shipped">Shipped</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            </div>
        </div>

        <form action="/order/update/@Model.OrderId" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Update" class="btn btn-primary" />
                </div>
            </div>
        </form>
    </div>
}
}
else
{
<h4>Order Has Been Shipped!</h4>
}

无论使用的语法是否正确,我的提交按钮都不起作用。当我按下按钮时,什么也没有发生,就好像我没有点击它一样,没有任何重定向。

一直工作到昨天。没有更改任何有关表单或相应控制器的代码。

这是表单对应的控制器。

[HttpGet]
    [Authorize(Roles = "ADM")]
    public ActionResult Update(Guid Id)
    {
        BusinessLayer.Orders order = new BusinessLayer.Orders();
        return View(order.GetOrder(Id));
    }

    [HttpPost]
    [Authorize(Roles = "ADM")]
    public ActionResult Update(CommonLayer.ORDER order, Guid id)
    {
        BusinessLayer.Orders blorder = new BusinessLayer.Orders();
        blorder.UpdateOrder(order);
        return RedirectToAction("UpdateDetails", new {id=id});
    }

【问题讨论】:

  • 使用@Html.BeginForm 并将@Model.OrderId 移动到&lt;input type="hidden"&gt;
  • 我已经在使用 @ 和 if,所以当我尝试将它与 using aswell 一起使用时会显示错误
  • 我的意思是,使用它而不是您的手动 &lt;form&gt; 标签。我没有注意到您顶部还有一个BeginForm,这使它成为一个嵌套表单,这不是一个有效的html。只需删除&lt;form&gt; 然后将路由参数放入BeginForm
  • 仍然没有工作 :( 这一直工作到昨天,我不知道会发生什么,因为我没有更改表单或相应控制器中的任何内容
  • 与这个问题本身无关,但对我来说,我的按钮不在using (Html.BeginForm) {} 内,记住它需要在里面。把它移到里面对我有用

标签: c# asp.net-mvc asp.net-mvc-4 model-view-controller


【解决方案1】:

使用下面的代码,如果不起作用,请告诉我们

添加以下行以传递 orderid

@Html.HiddenFor(model => model.OrderId)

更新以下行以调用控制器的操作

Html.BeginForm("update","order",FormMethod.Post)

终于去掉了不必要的表单标签

从视图中删除了 antiforgerytoken,因为您没有检查您的操作方法。 如果要添加,则需要在 Update Action 方法的 [httppost] 之后添加 [ValidateAntiForgeryToken]

@model CommonLayer.ORDER
    @{
    ViewBag.Title = "Update";
    Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h4>Update Order Status</h4>
    <br />

    @if (Model.OrderStatus != "Shipped")
    {
    using (Html.BeginForm("Update","Order",FormMethod.Post))
    {
        <div class="form-horizontal">
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.UserId)
            @Html.HiddenFor(model => model.OrderId)
            <div class="form-group">
                @Html.LabelFor(model => model.OrderId, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select class="form-control" name="OrderStatus">
                        <option value="Pending" id="pending">Pending</option>
                        <option value="Shipped" id="shipped">Shipped</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Update" class="btn btn-primary" />
                    </div>
                </div>
        </div>
    }
    }
    else
    {
    <h4>Order Has Been Shipped!</h4>
    }

【讨论】:

  • 您至少可以简要总结与原始代码相比所做的更改。
  • 复制了您的代码,但仍然无法正常工作:(昨天使用我发布的相同代码可以正常工作,突然提交按钮没有任何作用
  • 我会试试那个janina。感谢您的帮助。
  • 嗨,janina 我试着一点一点地评论,似乎当我排除 OrderDate 部分时,它可以工作。关于这是为什么的任何线索?
  • 什么都没有 我只是尝试按下更新按钮,没有任何反应,没有任何重定向或错误。非常感谢您的帮助伙伴
猜你喜欢
  • 2016-09-11
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
相关资源
最近更新 更多