无法使用@ Html.BeginForm将值发布回操作方法

时间:2015-01-14 12:46:16

标签: razor model-view-controller knockout.js

我有一些代码,当用户点击" x" icon然后调用CancelPendingQuote操作方法,传递requestUrl中的requestId。操作方法正在命中,但该值未包含在requestIdEncrypted参数中,因此操作方法参数具有空值。

待处理清单

@using (@Html.BeginForm("CancelPendingQuote", "Quote", new { requestIdEncrypted = request.RequestIdEncrypted }, FormMethod.Get, new { enctype = "multipart/form-data", @id = "removeRequest" }))
{
  <span data-bind="click: function(data, event){  userWarning_Dialog('#removeRequest_dialog', event); }">

  <img src="~/Areas/Waybill/Content/Images/Normal_Size/posta_delete_20px.png" />
  <img src="~/Areas/Waybill/Content/Images/Normal_Size/posta_delete_mouseover_20px.png" style="display:none" />

 </span>
}

提交表单的Knockout用户警告功能。当图像&#34; x&#34;点击。

removeRequest: function (model, event)
    {
        var form = $("#removeRequest").closest("form");

        $(form).submit().error(function (messageObj) {
            // if fail return error message
            $(".information-bar").trigger("ErrorText", messageObj.message);               
        });
        $("#removeRequest_dialog").dialog("close");

    },

行动方法

    [Authorize]
      public ActionResult CancelPendingQuote(string requestIdEncrypted)
    {
        int requestId = Convert.ToInt16(Decryption.Decrypt(requestIdEncrypted));

        _controllerContent.QuoteService.Value.CancelPendingQuoteRequest(requestId);
        return RedirectToAction("Dashboard");
    }  

任何想法?

1 个答案:

答案 0 :(得分:0)

这里有几件事。首先,您需要确保发布到服务器的对象的名称与Controller的参数匹配。例如,如果你发送这个Javascript对象:

{ requestIdEncrypted: "exampleString" }

{ requestIdEncrypted: viewModel.requestId() }

然后你的Controller方法应该接受输入。

其次,从您的代码中我不清楚数据是如何发布的。 $(form).submit().error(function (messageObj)有点令人困惑:这条线是否负责提交表格?如果表单提交不成功,它是否会被调用?它有效吗?这是not clear to me what you're trying to do with this。您可能必须找出将错误处理程序附加到表单的另一种方法,如果这是您正在尝试执行的操作 - 除非它正常工作。

相关问题