Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC sending data from View to Controller

Tags:

asp.net-mvc

I am quite new to MVC 3.

I know how to send a strongly typed object from a Controller to a View. What I have now, is a View which contains a table/form which consists of that data.

The user can change that data whilst they're are in that View (html page).

When they click on "Save", how do I send the data from the View back to the Controller so that I can update my database.

Do I overload the Controller method so that it accepts a parameter of the model type? Can you please provide some source code.

(Please do not show code of persisting data to a database, I know how to do that part).

Thank you very much for helping me.

I would also prefer using @Html.BeginForm()

like image 411
Subby Avatar asked Aug 29 '12 15:08

Subby


2 Answers

I like creating an action method made for my post data. So let's say you have a UserViewModel:

public class UserViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Then a UserController:

public class UserController
{
    [HttpGet]
    public ActionResult Edit(int id)
    {
        // Create your UserViewModel with the passed in Id.  Get stuff from the db, etc...
        var userViewModel = new UserViewModel();
        // ...

        return View(userViewModel);
    }

    [HttpPost]
    public ActionResult Edit(UserViewModel userViewModel)
    {
        // This is the post method.  MVC will bind the data from your
        // view's form and put that data in the UserViewModel that is sent
        // to this method.

        // Validate the data and save to the database.

        // Redirect to where the user needs to be.
    }
}

I'm assuming you have a form in your view already. You'll want to make sure that the form posts the data to the correct action method. In my example, you'd create the form like so:

@model UserViewModel

@using (Html.BeginForm("Edit", "User", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.Name)
    @Html.HiddenFor(m => m.Id)
}

The key to all this is the model binding that MVC does. Make use of the HTML helpers, like the Html.TextBoxFor I used. Also, you'll notice the top line of the view code I added. The @model tells the view you'll be sending it a UserViewModel. Let the engine do work for you.

Edit: Good call, did that all in Notepad, forgot a HiddenFor for the Id!

like image 164
Gromer Avatar answered Oct 14 '22 01:10

Gromer


In MVC, the act of scraping out data from POST or GET HttpRequests is referred to as Model Binding - there are plenty of SO questions relating to this.

Out of the box, MVC will bind your Get and Post variables based on convention, e.g. a form field with the name 'FormName' will be bound back to a parameter on your controller with the same name.

Model binding also works for objects - MVC will instantiate an object for your controller, and set the properties with the same name as your form.

like image 28
StuartLC Avatar answered Oct 14 '22 00:10

StuartLC