0

我在这里查看了几篇帖子以寻求解决方案。我会尝试一个新的问题。

我的表格:

<form method="post" action="@Url.Action("Manage")"  data-cs-ajax="true" data-cs-target="#catalogList" >

它有一个自动完成字段和分页,非常类似于 Scott Allen 的 OdeToFood 示例。

有 :

<input type="submit" value="Update" class="btn btn-default" />

它将一些过滤的项目更新为从中选择 - 一切都很好 - 而且:

<input type="submit" name="btnSave" id="btnSave" value="Save" class="btn btn-default" />

他们都成功命中:

//POST: /WebCatalog/Manage/5
[HttpPost, ActionName("Manage")]
[ValidateAntiForgeryToken]
public ActionResult Manage([Bind(Include = "Id,CatalogId,CatalogColourId,CatalogSizeId,CatalogCategoryId,CatelogSupplierId,StartDate,EndDate,Active,isSubmitted")] WebCatalogViewModel model, string searchTerm = null, int page = 1)
    { ...

由于 javascript,它们确实触发了控制器操作:

$(function () {

var ajaxFormSubmit = function () {
    var $form = $(this);

    var options = {
        url: $form.attr("action"),
        type: $form.attr("method"),
        data: $form.serialize()
    };

    $.ajax(options).done(function (data) {
        var $target = $($form.attr("data-cs-target"));
        var $newHtml = $(data);
        $target.replaceWith($newHtml);
        var options = {};
        $newHtml.effect("highlight", options, 500, doneEffect);
    });

    return false;
};

现在...

我想在模型中设置一个字段,让控制器知道第二个 btnSave 按钮被点击了……当然是保存记录……

苏...

$(function () {

    $("#btnSave").click(function () {

        $("isSubmitted").val("1");
        return true;
    });

});

这会触发:在 ajaxFormSubmit 中通过 ajax 提交页面之前,$(#btnSave") 的按钮 jQuery 代码会成功触发。

但是,在控制器动作中,isSubmitted 始终为 0。

我试过了 :

$("isSubmitted").val(1);

我已尝试将表单设置为 method='get' ,但仍然没有发布 - 不过,我可能会尝试在 btnSave.click js 函数中使用 javascript 更改方法 - 不知道该怎么做......如果可以的话t 在表单控件中保存值,那为什么我可以更改表单方法?

我试过 $Ajax.ActionLink - 但总是得到 500 IIS 错误。

基本上,我需要一种方法来知道用户点击了保存按钮,而不是更新按钮。

任何建议表示赞赏!

4

3 回答 3

3

You don't need a hidden value to tell the controller which button was clicked. What you need to do is name the 2 buttons as follow

<input type="submit" name="btnAction" value="Update" class="btn btn-default" />
<input type="submit" name="btnAction" value="Save" id="btnSave" class="btn btn-default" />

As you can see, they have the same name but different values (update vs save).

In the controller, add another parameter called btnAction, which is the name of the buttons, to the action.

[HttpPost, ActionName("Manage")]
[ValidateAntiForgeryToken]
public ActionResult Manage(string btnAction, [Bind(Include = "Id,CatalogId,CatalogColourId,CatalogSizeId,CatalogCategoryId,CatelogSupplierId,StartDate,EndDate,Active,isSubmitted")] WebCatalogViewModel model, string searchTerm = null, int page = 1)
{
     if(btnAction.ToLower() == "save")
     {
          // The save button was clicked
     }
     else if(btnAction.ToLower() == "update")
     {
          // The update button was clicked
     }
     else
     {
          // default ...
     }
}

Basically, the parameter btnAction will get the value of the submit button triggering the form to get post.

Hope this help.

于 2014-04-13T23:26:56.743 回答
0

@Robert Achmann,这是我的答案

似乎 $("isSubmitted") 中的选择器丢失了,例如 $("#isSubmitted") 用于 id isSubmitted 的输入。

于 2014-04-14T00:07:12.607 回答
0

您可以在表单中使用隐藏字段来存储单击的字段的值(使用 jquery 事件处理程序控制它),为此您可以将按钮设置为 type="button" 而不是 type="submit" (为了从您的 Jquery 代码提交您的表单)或者您可以使用 eventPreventDefault。

好吧,这是一个可能对您有所帮助的代码示例:

    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#bt1").click(function(){
        alert(' Save button');
        $("#operation").val('save');
        $("#frm").submit();
      });

      $("#bt2").click(function(){
        alert(' Update button');
        $("#operation").val('update');
        $("#frm").submit();
      });
    });
    </script>
    </head>

    <body>
       <form id="frm">
        <div id="div1"><h2> Form buttons - What of then was clicked? </h2></div>
        <button id="bt1"> Save button </button>
        <button id="bt2"> Update button </button>
         <input type="hidden" id="operation" name="operation" value=""/>
        </form>     
        </body>
        </html>
于 2014-04-13T22:17:59.903 回答