Hi I am pretty new to web development and am stuck on a specific scenario.
I have a Map controller with 2 methods:
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.
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.
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.
Does anyone have a better idea of how I should do this?
The Action method for POST operation accepts an object of the PersonModel class as parameter. The values posted from the Form inside the View are received through this parameter. Next step is to add a View for the Controller and while adding you will need to select the PersonModel class created earlier.
In order to add a Web API Controller you will need to Right Click the Controllers folder in the Solution Explorer and click on Add and then Controller. Now from the Add Scaffold window, choose the Web API 2 Controller – Empty option as shown below. Then give it a suitable name and click OK.
Under the value, we can pass our response data in whichever format we want to return the data. I want to call Post method. Hence, select POST method from the list of HTTP methods. We try and pass the Web API URL in the URL tab in order to make sure you need to first run your project and pass localhost URL.
cs to determine the controller and action from the url. The default route is {controller}/{action}/{id} where the first two have defaults of "Home" and "Index", respectively, and the third is optional. You can change this if you want by adding/modifying the route set up.
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"});
}
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
. 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(). Inside the BeginForm, you can have a HTML Input Button of type Submit to make the POST call to Controller action.
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 -
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 -
@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.
You are sending a GET request. Because you are mapping your parameters on the 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 HttpPost in your AJAX request.
Edit: since you are using POST request should be like this
$.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'); }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With