如何知道哪个元素调用ajax请求

时间:2013-03-01 12:52:22

标签: jquery asp.net-mvc-4

我只想获取调用ajax请求的html元素的id,所以我可以在控制器函数上使用这些数据。

这是观点:

   @model CmsSite.Models.SideBarData

@{
    ViewBag.Title = "Home Page";
}
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
        </div>
    </section>
}
<h3>We suggest the following:</h3>

<aside id="sidebar">
 @using (Html.BeginForm("ShowContent", "SideBar"))
 {  
         <input id="side_bar_manage_categoty" name="side_bar_manage_categoty" class="post-transaction" type="submit" value="Manage Categorys" />
         <input name="side_bar_manage_products" class="post-transaction" type="submit" value="Manage Products" />
         <input name="side_bar_manage_users" class="post-transaction" type="submit" value="Manage Users" />
         <input name="side_bar_Watch_sales" class="post-transaction" type="submit" value="Watch Sales" />
         <input name="side_bar_change_site_appearance" class="post-transaction" type="submit" value="Change Site Appearance" />
 }
        </aside>  
<div ><p class="content"></p></div>

  <script id="ajax-request-result" type="application/html-template">             
        <div class="content">{{request_result}}</div>
     </script>

@section Scripts {
    <script type="text/javascript">
        $(function () {
            $('.post-transaction').on("click", function () {

                var form = $(this).parent("form");

                $.ajax({
                    type: "POST",
                    url: form.attr('action'),
                    data: form.serialize()
                })
                    .success(function (data) {
                        var template = $("#ajax-request-result").clone().html();

                        var html =
                            template
                                    .replace("{{request_result}}", data.request_result);
                       $(".content").replaceWith(html);

                    })
                    .error(function () {
                        alert("Your bid has been rejected");
                    });

                return false;
            });
        });
    </script>
}

这是获取ajax请求的方法的控制器:

[HttpPost]
        public ActionResult ShowContent(string side_bar_manage_categoty)
        {

            string a = side_bar_manage_categoty;
            return Json(new
            {
                request_result = "Transaction Failed!"
            });
        }

enter image description here

1 个答案:

答案 0 :(得分:0)

表单元素需要name个属性。所以如果你做这样的事情:

<input id="side_bar_manage_categoty" name="side_bar_manage_categoty" class="post-transaction" type="submit" value="Manage Categorys" />
<input id="side_bar_manage_products" name="side_bar_manage_products" class="post-transaction" type="submit" value="Manage Products" />

然后,这些名称(我刚从id属性中复制的名称)将携带值到服务器上的FormCollection。所以,如果您的行为如下:

public ActionResult ShowContent(FormCollection collection)

然后,您可以检查操作方法中的collection对象,以查看它是否具有页面上任何特定按钮的键/值。您甚至可以更进一步,使用MVC的表单绑定来创建强类型的操作方法参数:

public ActionResult ShowContent(string side_bar_manage_categoty, string side_bar_manage_products)

在这种情况下,如果您点击side_bar_manage_products的按钮,那么string将包含值"Manage Categorys"(因为那是value中的input属性}),另一个字符串将是null。如果单击其他按钮,则相反。所以你只需要测试非空字符串(string.IsNullOrWhiteSpace()是测试它的好方法),这将是调用的按钮。