我只想获取调用 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!"
});
}