1

I'm new to ASP.NET MVC. I'm trying to update model on button click with no success: every time I push the button an HttpGet controller method is invoked.

Here is my markup

@model DataInterface.Model.Entry

<button onclick="location.href='@Url.Action("Survey")'">Finish survey</button>

Here is Controller code

[HttpGet]
public ActionResult Survey()
{
    var entry = new Entry();
    return View(entry);
}

[HttpPost]
public ActionResult Survey(Entry newEntry)
{
       // save newEntry to database
}

When I click button HttpGet method is invoked. Why?


It is bad to be a rookie) Thanks to all!

1
  • well, that's the exact thing you tell the browser to do. On button click, the URL of the browser is changed to point to the Survey method in your controller. The browser will send a get request to it and process the response. The easiest way to send a POST request is by submitting a form. Commented Jan 27, 2015 at 15:59

3 Answers 3

2

If you access a URL without explicitly specifying the HTTP method, ASP.NET MVC will assume a GET request. To change this, you can add a form and send it:

@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    <input type="submit" value="Finish survey" />    
}

If you do this, your POST method will be invoked. The Entry parameter, however, will be empty, since you do not specify any values to send along with the request. The easiest way to do so is by specifying input fields, e.g. text inputs, dropdown, checkboxes etc.

@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.Title)
    <input type="submit" value="Finish survey" />    
}

If you have the object stored on the server somewhere and only want to finish it off by writing it into the database or changing its status, you could pass the Id of the object (or some temporary Id) along the post request and make the controller method work only with the Id:

@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    @Html.HiddenFor(m => m.Id)
    <input type="submit" value="Finish survey" />    
}

[HttpPost]
public ActionResult Survey(Entry newEntry)
{
    // newEntry.Id will be set here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Side note: Since the view was generated by the Survey() method, then all that's needed is @using (Html.BeginForm()) {... - its only necessary to include the parameters if you want to override the defaults
0
   @using (Html.BeginForm("Survey", "<ControllerName>", FormMethod.Post))
   {
       <input  type="submit" value="Finish survey" />    
   }

Comments

0

you must declare your form

@model DataInterface.Model.Entry
@using (Html.BeginForm("action", "Controlleur", FormMethod.Post, new {@class = "form", id = "RequestForm" }))
{
<input  type="submit" value="Finish survey" />
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.