【发布时间】:2018-06-30 05:46:27
【问题描述】:
这是我的视图代码,其中 ActionLink 方法将数据发布到控制器的 ActionResult 方法:
@foreach (var item in Model)
{
<tr>
<td>@item.Deal_Date</td>
<td>@item.Total_Amount_Remaining</td>
<td>@item.Dealer_Name</td>
<td>@item.Validity_Date</td>
<td>@item.Location</td>
<td>@item.Deal_Amount</td>
<td>@item.Client_Info.Client_Name</td>
<td>@Html.ActionLink("Recieve payment", "RecievePayment", new { id = item.Deal_ID })</td>
</tr>
}
在上面的代码中,@Html.ActionLink 方法通过 Route Values 参数将 'id' 发送到 Controller 的 'ReceivePayment' Action 方法。以下是我的控制器的 Action Method 的代码:
public ActionResult RecievePayment(decimal id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Sell_Deal sell_deal = db.Sell_Deal.Find(id);
if (sell_deal == null)
{
return HttpNotFound();
}
ViewBag.Client_ID = new SelectList(db.Client_Info, "Client_ID", "Client_Name", sell_deal.Client_ID);
return View(sell_deal);
}
我想用 Ajax.ActionLink 方法实现同样的效果。以下是我的 Ajax 方法在视图中的代码:
<script>
$(document).ready(function () {
$("#BtnSearch").click(function () {
var SearchBy = $("#SearchBy").val();
var SearchValue = $("#Search").val();
var SetData = $("#DataSearching");
SetData.html("");
debugger;
$.ajax({
type: "POST",
contentType: "html",
url: "/SelectDeal/GetSearchingData?SearchBy=" + SearchBy + "&SearchValue=" + SearchValue,
success: function (result) {
debugger;
if (result.length == 0) {
SetData.append('<tr style="color:red"><td colspan="3">No Match Data</td></tr>');
}
else {
$.each(result, function (i, item) {
//var clientName = item.
var DealDateString = item.Deal_Date;
var valDealDate = new Date(parseInt(DealDateString.replace(/(^.*\()|([+-].*$)/g, '')));
var finalDealDate = valDealDate.getMonth() + 1 + "/" + valDealDate.getDate() + "/" + valDealDate.getFullYear();
var ValidityDateString = item.Validity_Date;
var valValidityDate = new Date(parseInt(ValidityDateString.replace(/(^.*\()|([+-].*$)/g, '')));
var finalValidityDate = valValidityDate.getMonth() + 1 + "/" + valValidityDate.getDate() + "/" + valValidityDate.getFullYear();
var val = '<tr>' +
'<td>' + finalDealDate + '</td>' +
'<td>' + item.Total_Amount_Remaining + '</td>' +
'<td>' + item.Dealer_Name + '</td>' +
'<td>' + finalValidityDate + '</td>' +
'<td>' + item.Location + '</td>' +
'<td>' + item.Deal_Amount + '</td>' +
'<td> @Ajax.ActionLink("Receive Payment", "RecievePaymentAjax", new { id = item.Deal_ID }, new AjaxOptions { HttpMethod = "POST" }) <td>' +
'</tr>';
SetData.append(val);
});
}
},
error: function (data) {
alert(data);
}
});
});
});
在上面的代码中,我想将“Id”值发布到我的控制器的“RecievePaymentAjax”Action Method 以进行进一步处理,但在 Ajax.ActionLink 的路由值参数中,我收到错误“名称项确实”在当前上下文中不存在'。我怎么解决这个问题?如果有任何其他方法可以实现这一点,请建议我。
【问题讨论】:
-
嗨,使用
data属性,就像$.ajax({..., data:{param1:value1,param2:value2} ,...})
标签: c# jquery ajax asp.net-mvc razor