MVC:使用 Jquery.ajax() 将参数传递给动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13100198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC: pass parameter to action using Jquery.ajax()
提问by Mayank Pathak
I'm pretty new to MVC
. I need to make ajax
call to an Action
with parameters using html.Action()
. I'm unable to pass this..
我对MVC
. 我需要ajax
调用的Action
使用参数html.Action()
。我无法通过这个..
Hope it will be helpful for other MVC begineers as well..
希望它对其他 MVC 初学者也有帮助..
HTML:
HTML:
<%: Html.ActionLink("Add Race", "AddRace",
new {eventId = Model.EventId, fleetId=Model.SelectedFleet.ID},
new{@onclick=string.Format("return checkFleetAddedandScroing()")}) %>
Jquery:
查询:
function checkFleetAddedandScroing() {
debugger;
$.ajax({
type: "GET",
url: '<%=Url.Action("CheckFleetExists")%>',
dataType: "json",
cache: false,
success: function (data, textStatus) {
data = eval("(" + data + ")");
if (data == true) {
return true;
}
else {
alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
return false;
}
}, //success
error: function (req) {
}
});
}
Action:
行动:
public JsonResult CheckFleetExists(Guid fleetId )
{
bool exists = false;
try
{
exists = !db.Races.Any(r => r.FleetID == fleetId);
}
catch
{
}
return Json(exists, JsonRequestBehavior.AllowGet);
}
I need to pass fleetid
to action which is in Model.SelectedFleet.ID
. Its being used somewhere on pages. But i'm unable to use that somehow..
我需要传递fleetid
到Model.SelectedFleet.ID
. 它在页面上的某处使用。但我无法以某种方式使用它..
please suggest where i'm doing wrong...
请建议我哪里做错了...
回答by Darin Dimitrov
It looks like you are trying to invoke a controller action using AJAX when the link is clicked and depending on the result of this call either allow the user to be redirected to the actual AddRace
action or be prompted with an error message.
看起来您正在尝试在单击链接时使用 AJAX 调用控制器操作,并且根据此调用的结果,允许将用户重定向到实际AddRace
操作或提示错误消息。
The problem with your code is that you are attempting to return true/false from within the success AJAX callback which doesn't make any sense. You need to always return false from the click callback and inside the success callback, depending on the returned value from the server, redirect manually using the window.location.href
function.
您的代码的问题在于您试图从没有任何意义的成功 AJAX 回调中返回真/假。您需要始终从单击回调和成功回调中返回 false,根据服务器的返回值,使用该window.location.href
函数手动重定向。
HTML:
HTML:
<%: Html.ActionLink(
"Add Race",
"AddRace",
new {
eventId = Model.EventId,
fleetId = Model.SelectedFleet.ID
},
new {
data_fleetid = Model.SelectedFleet.ID,
@class = "addRace"
}
) %>
Jquery:
查询:
<script type="text/javascript">
$(function () {
$('.addRace').click(function (evt) {
$.ajax({
type: 'GET',
url: '<%= Url.Action("CheckFleetExists") %>',
cache: false,
data: { fleetId: $(this).data('fleetid') },
success: function (data) {
if (data.exists) {
// the controller action returned true => we can redirect
// to the original url:
window.location.href = url;
}
else {
alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
}
},
error: function (req) {
}
});
// we make sure to cancel the default action of the link
// because we will be sending an AJAX call
return false;
});
});
</script>
Action:
行动:
public ActionResult CheckFleetExists(Guid fleetId)
{
bool exists = false;
try
{
exists = !db.Races.Any(r => r.FleetID == fleetId);
}
catch
{
}
return Json(new { exists = exists }, JsonRequestBehavior.AllowGet);
}
Remark: inside your AddRace
controller action don't forget to perform the same verification as you are doing inside your CheckFleetExists
. The user could simply disable javascript and the AJAX call will never be done.
备注:在您的AddRace
控制器操作中不要忘记执行与您在CheckFleetExists
. 用户可以简单地禁用 javascript 并且永远不会完成 AJAX 调用。
回答by Behnam Esmaili
change your action like this :
像这样改变你的行动:
public JsonResult CheckFleetExists(string fleetId)
{
var fleetGuid = Guid.Parse(fleetId);
bool exists = false;
try
{
exists = !db.Races.Any(r => r.FleetID == fleetGuid );
}
catch
{
}
return new JsonResult{ Data = exists};
}
and change you ActionLink so :
并将您的 ActionLink 更改为:
<%: Html.ActionLink("Add Race", "AddRace",
new {eventId = Model.EventId, fleetId=Model.SelectedFleet.ID},
new{onclick=string.Format("return checkFleetAddedandScroing({0})",Model.SelectedFleet.ID)}) %>
and your script block may look something like this :
您的脚本块可能如下所示:
function checkFleetAddedandScroing($fleetId) {
$.ajax({
type: "POST",
url: '<%=Url.Action("CheckFleetExists")%>',
dataType: "json",
data : { "fleetId" : $fleetId },
cache: false,
success: function (data, textStatus) {
data = eval("(" + data + ")");
if (data == true) {
return true;
}
else {
alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
return false;
}
}, //success
error: function (req) {
}
});
}
回答by Mayank Pathak
Problem was in answers that url to action was not complete..i did it using this way
问题在于操作的 url 不完整的答案..我是用这种方式做的
function checkFleetAddedandScroing() {
// debugger;
$.ajax({
type: "POST",
url: '<%=Url.Action("CheckFleetExists", new {eventId=Model.EventId})%>',
dataType: "json",
cache: false,
success: function (data, textStatus) {
data = eval("(" + data + ")");
if (data == true) {
return true;
}
else {
alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
return false;
}
}, //success
error: function (req) {
}
});
}