Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC How to call controller post method

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?

like image 926
NAJ Avatar asked Oct 06 '14 09:10

NAJ


People also ask

How can call POST action method on button click in MVC?

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.

How do you POST to a Web API controller from an MVC controller?

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.

How can we call post method in Web API from url?

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.

How do you call a controller method from HTML?

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.


3 Answers

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"});

}
like image 198
M.Azad Avatar answered Sep 18 '22 06:09

M.Azad


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.

like image 21
ramiramilu Avatar answered Sep 18 '22 06:09

ramiramilu


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'); }
    });
like image 36
Manjar Avatar answered Sep 20 '22 06:09

Manjar