MVC How to call post post method
Hi I am fairly new to web development and am following a certain scenario.
I have a card controller with two methods:
public ActionResult Map1(double easting, double northing)
public ActionResult Map2(double easting, double northing)
When called, they both move through the appropriate view with either model:
return View(model);
Then I have a javascript that should call the appropriate controller method based on the action passed.
I want to mark the controller methods as [HttpPost], but when I do, then use the ajax request in javascript that caused the View call and the page is not redirected.
Currently the only way to get me to work is:
window.location.href = '/Map/' + actionVal + '?easting=' + easting + '&northing=' + northing;
But using this, I cannot set the methods as POST.
Does anyone have a better idea of โโhow I should go about doing this?
source to share
Does anyone have a better idea of โโhow I should go about doing this?
You don't actually need to have different controller actions TWO
, you can only have instead ONE
. And this action will return the view you want to display.
One way to do POST is to use HTML.BeginForm()
and pass controller and action names along with FormMethod.POST
in BeginForm (). Inside the BeginForm, you can introduce an HTML input button of type Submit to make the POST call a controller action.
Also, if you want to differentiate the call to this controller action, I would suggest you something like this -
First, create a model like this one with a variable Type
through which you can distinguish the operation to be performed on the data -
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 define your view HiddenField
like this:
@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 in the view, set Type
so that you can distinguish between post operation and do the necessary calculations on your data and return the view.
source to share
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"});
}
source to share
You are sending a GET request . Since you are mapping your parameters to a String.
If you want to use it like this, you must add an attribute [HttpGet]
to the action. But I would recommend that you use HttpPost in your AJAX request .
Edit: since you are using a POST request should be like
$.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'); }
});
source to share