Javascript MVC 如何调用控制器 post 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26212987/
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 How to call controller post method
提问by NAJ
Hi I am pretty new to web development and am stuck on a specific scenario.
嗨,我对 Web 开发很陌生,并且被困在特定场景中。
I have a Map controller with 2 methods:
我有一个带有两种方法的 Map 控制器:
public ActionResult Map1(double easting, double northing)
public ActionResult Map2(double easting, double northing)
When called they both navigate their corresponding view with whatever model is required:
当被调用时,它们都使用所需的任何模型导航其相应的视图:
return View(model);
I then have some javascript which needs to call the corresponding controller method depending on the action passed through.
然后我有一些 javascript 需要根据传递的动作调用相应的控制器方法。
I want to mark the controller methods as [HttpPost], but when I do this then use an ajax request in the javascript the call to the View gets swallowed and the page is not redirected.
我想将控制器方法标记为 [HttpPost],但是当我这样做时,然后在 javascript 中使用 ajax 请求,对 View 的调用被吞并并且页面没有被重定向。
Currently the only way I have got it to work is by this:
目前,我让它工作的唯一方法是:
window.location.href = '/Map/' + actionVal + '?easting=' + easting + '&northing=' + northing;
But with using this I cannot set the methods as POST.
但是使用这个我不能将方法设置为 POST。
Does anyone have a better idea of how I should do this?
有没有人对我应该如何做到这一点有更好的想法?
采纳答案by ramiramilu
Does anyone have a better idea of how I should do this?
有没有人对我应该如何做到这一点有更好的想法?
Actually there is no need for you to have TWOdifferent Controller actions, instead you can have only ONE. And this action is going to return a view which you want to display.
实际上没有必要让你有TWO不同的 Controller 动作,相反你可以只有ONE. 这个动作将返回一个你想要显示的视图。
One way to make a POST is to use HTML.BeginForm()and pass Controller and Action names along with FormMethod.POSTto BeginForm(). Inside the BeginForm, you can have a HTML Input Button of type Submit to make the POST call to Controller action.
进行 POST 的一种方法是使用HTML.BeginForm()Controller 和 Action 名称并将其传递FormMethod.POST给 BeginForm()。在 BeginForm 中,您可以使用 Submit 类型的 HTML 输入按钮来对 Controller 操作进行 POST 调用。
Also if you want to differentiate the invocation of this controller action, I would suggest you something like this -
另外,如果您想区分此控制器操作的调用,我建议您这样做-
First construct you model like this with Typevariable through which you can differentiate the operation you need to perform on data -
首先用Type变量构建你的模型,通过它你可以区分你需要对数据执行的操作 -
public class Details
{
public string easting { get; set; }
public string northing { get; set; }
public string type { get; set; }
}
And let your controller action be defined like this -
让你的控制器动作像这样定义 -
[HttpPost]
public ActionResult Map(Details details)
And you can have your view define a HiddenFieldlike this -
你可以让你的观点定义一个HiddenField这样的 -
@model Namespace.Details
@{
ViewBag.Title = "Title";
}
<div id="uploadCourseList">
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
@* Other properties of Model *@
@Html.HiddenFor(m => m.type)
<input type="submit">
}
</div>
Now on the view set the type, so that you can differentiate the post operation and perform necessary calculations on your data and return the view.
现在在视图上设置type,以便您可以区分后期操作并对数据执行必要的计算并返回视图。
回答by M.Azad
you can use this code:
您可以使用此代码:
//Client Side
$.ajax({
type: "POST",
url: '@Url.Action("FirstAjax", "AjaxTest")',
contentType: "application/json; charset=utf-8",
data: {id :1},
dataType: "json",
success: function(result) {
alert(result);
window.locationre=result.url;
}
});
//AjaxTest Controller
[HttpPost]
public ActionResult FirstAjax(string id)
{
return Json(new {url="URL"});
}
回答by Manjar
You are sending a GETrequest. Because you are mapping your parameters on the queryString.
您正在发送GET请求。因为您正在将您的参数映射到queryString 上。
If you want to use it like that you should add the [HttpGet]attribute to the action. But I'd rather recomend you to use HttpPostin your AJAXrequest.
如果您想像这样使用它,您应该将[HttpGet]属性添加到操作中。但我宁愿建议您在AJAX请求中使用HttpPost。
Edit: since you are using POST request should be like this
编辑:因为你使用 POST 请求应该是这样的
$.ajax({
type: "POST",
url: '/Maps',
contentType: "application/json; charset=utf-8",
data: {easting: easting, northing: northing}, //Maps the controller params
dataType: "json",
success: function() { alert('Success'); }
});

