0
我只想獲取調用ajax請求的html元素的id,所以我可以在控制器函數中使用這些數據。如何知道哪個元素調用ajax請求
這是視圖:
@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!"
});
}
我嘗試這樣做,就像您的第二種方法 – user1310492 2013-03-01 13:16:40
但 string side_bar_manage_categoty = null; – user1310492 2013-03-01 13:17:09
@ user1310492:您是否已將'name'屬性添加到'input'?另外,鑑於拼寫錯誤,您是否始終保持拼寫名稱?它在方法簽名中必須與'name'屬性中的相同。 – David 2013-03-01 13:18:18