[英]MVC How to call controller post method
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控制器有2种方法:
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? 有没有人更清楚我应该怎么做?
Does anyone have a better idea of how I should do this? 有没有人更清楚我应该怎么做?
Actually there is no need for you to have TWO
different Controller actions, instead you can have only ONE
. 实际上你没有必要拥有TWO
不同的Controller动作,而你只能拥有ONE
。 And this action is going to return a view which you want to display. 此操作将返回您要显示的视图。
One way to make a POST is to use HTML.BeginForm()
and pass Controller and Action names along with FormMethod.POST
to BeginForm(). 制作POST的一种方法是使用HTML.BeginForm()
并将Controller和Action名称与FormMethod.POST
一起FormMethod.POST
给BeginForm()。 Inside the BeginForm, you can have a HTML Input Button of type Submit to make the POST call to Controller action. 在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 Type
variable 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 HiddenField
like 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
,以便您可以区分后期操作并对数据执行必要的计算并返回视图。
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"});
}
You are sending a GET request. 您正在发送GET请求。 Because you are mapping your parameters on the queryString . 因为您要在queryString上映射您的参数。
If you want to use it like that you should add the [HttpGet]
attribute to the action. 如果你想像那样使用它,你应该将[HttpGet]
属性添加到动作中。 But I'd rather recomend you to use HttpPost in your AJAX request. 但我宁愿建议你在你的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'); }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.