What's the best way to pass both a model and FormCollection data to my MVC 3 post controller
Normally, when receiving form data in a controller, I'd use:
public ActionResult Edit(int id, FormCollection formValues)
I have a view which is bound to a model but also includes some items not attached to the model which I thought I'd be able to access vi开发者_运维知识库a FormCollection as
public ActionResult Edit(int id, MyModel objModel, FormCollection formValues)
I wondered if this is the best way to achieve this or if there's another to code this.
If this is a post, you will need to tag the method with the HttpPost
attribute.
[HttpPost]
public ActionResult Edit(int id, FormCollection formValues)
{
var objModel = new MyModel();
// updates model here
TryUpdateModel(objModel);
}
In your view, you would have something like:
@model MyModel
... some html here
<fieldset>
<legend>MyModel Information</legend>
@Html.EditorForModel()
</fieldset>
So you can edit the model fields using the EditorForModel
and retrieve those values using TryUpdateModel
. Noting that you still have access to your FormCollection
object.
This isn't the way to work. You shouldn't use your database model directly into your View.
You should pass a ViewModel. This way you can pass your model and extra properties.
public class MyViewModel
{
// All the properties for your model.
// All other properties needed and not attached to your model
}
In the Post action in your controller you can receive the MyViewModel and just make your model with the properties inside the ViewModel.
Check out this post for more information:
http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx
Before you get any further, read about difference between a domain model and a view model. Quite often, in a simple application you can get away with using a domain model. As the system grows, you are likely to encounter problems where simple features will be difficult to implement. Eventually you'll see a need to refactor code and this is when you are likely to see a need for a view model.
Your question is regarding parameters that are passed to controller actions. You have a controller to edit something.
There is an Edit GET where you do something like this:
- Read data from database
- Wrap it into the object
- Pass this object to a view which will display the object
There is an Edit POST want to recieve data and write changes to the database. So what do you need to make that happen? You need id and a combination of fields that you wish to update. Passing FormCollection and something else means that you have to map values from FormCollection to your domain model. This is time consuming and a repetative task.
You don't want to do this, so you want submit back a single object, doesn't matter whether it's a view model or a domain model, but it should be an object that contains all the data that you need for an update.
Data binding feature of the MVC 3 framework will automatically map fields from the form to your model using clever reflection.
What you are doing is kind of works and it's a great way to learn. So don't give up. Refactor, see whether you can pass a single object and work with that.
Good luck
精彩评论