asp.net mvc - bootstrapping object via model binder - asp.net-mvc

I have a domain object Thing which can contain several Categories. So I have implemented my HTML helper to create a checkbox group of all possible Categories. I have no problem receiving:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Thing Thing, List<string> Categories)
However I am wondering whether I could use a custom Model binder to use just this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Thing Thing)
So basically I am looking for a way to use the model binder to bootstrap the object tree/graph.
Any pointers appreciated. Thanks.
Christian

I was mixing up domain objects and DTOs. Making Thing a DTO (or view object?) does the trick.
so having something like this works:
class Thing
(
List Categories;
)
The model binder figures out how to put the posted data into the Categories

Related

Get an Entity in Save Method, What is correct form?

I'm begginer in asp.net mvc and i have some doubts.
P.S: I'm using DDD to learn
I have an ACtion in a Controller and it'll save an entity (from my model) by a repository (for a database).
My doubts is, How can I get the informations from the View and save it by a repository in my Controller ?
Is it correct to get an entity of my Model in Save method of controller, like this:
public ActionResult Save(Product product)
{
// validate object
// save data in repository
return View("Success");
}
Or Need I get an DTO (with a structure similar to my entity) and create an object passing property by property to an entity ?
I didnt' like of FormCollection and I'd like to know, What is recommended architecturally ?
Thanks a lot guys
Cheers
I you want to follow DDD practices as described by the Blue Book you should bind your views to DTO's which can be forwarded to a thin 'Application' layer where Domain objects are created or retrieved from database. This application layer can be a simple facade with methods or can leverage command pattern.
For a live demo, you can see my project -- DDDSample.NET
This kind of problem can be fixed by adding so called view model.
Basically - a view model is DTO that provides data for particular view. In similar fashion - view models are used to get data back from view via model binding. Then - controller just forwards necessary data to domain model.
Typically, in ASP.NET MVC your controller actions will receive strongly typed objects returned by the DefaultModelBinder when editing entity types. Using this pattern you can pass a "Product" to your GET view either by itself or as part of a DTO and then your "Save" method would receive a "Product" object in its parameter list.
So long as you are using either editor templates or fields with matching names (i.e. Html.TextBox("Name") corresponds to Product.Name) then the DefaultModelBinder should be able to correctly fill the typed entity object passed to the action method. You shouldn't have to mess with the FormCollection except in certain edge cases.
[HttpGet]
public ActionResult Create() {
return View("Create", new Product());
}
[HttpPost]
public ActionResult Create(Product product) { //or Save(Product)
...
}
As long as your form has fields which match the fields in Product, they should automatically be populated for you based on the values. How you save the entity depends on the data model, whether you're creating a new record or editing an existing one, etc.

ASP.NET MVC2 - Custom Model Binder Examples

I am trying to find some examples of building a custom model binder for a unique binding scenario I need to handle, but all of the articles I found were for older versions of MVC which are no longer relevant in MVC2. I've been referencing the DefaultModelBinder source code to try to get a general feel for what I need to do, but it's entirely more complicated than my scenario and I'm having trouble isolating the specific logic I need to implement.
My goal is to take a collection of Checkbox/Textbox pairs and for all of the Checked pairs I would like to create a key/value pair of the Checkbox's value and the associated Textbox's value. After aggregating this data I need to do some string serialization on the collection so I can store it in a string property of the desired Model type. I already the data being sent from the form in a manageable format which will allow me to relate a given Checkbox to a specific Textbox, it's just a matter of figuring out how to get all the pieces where I need them.
Does anyone know of some up-to-date tutorials that can get me started with building a custom model binder?
I don't know why you think a lot has changed since MVC 1 regarding custom model binders. But If I understand what you are trying to do, it should be fairly easy.
public class CustomModelBinder : DefaultModelBinder {
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext) {
NameValueCollection form = controllerContext.HttpContext.Request.Form;
//get what you need from the form collection
//creata your model
SomeModel myModel = new SomeMode();
myModel.Property = "value";
//or add some model errors if you need to
ModelStateDictionary mState = bindingContext.ModelState;
mState.Add("Property", new ModelState { });
mState.AddModelError("Property", "There's an error.");
return myModel; //return your model
}
}
And your action :
public ActionResult Contact([ModelBinder(typeof(CustomModelBinder))]SomeModel m){
//...
}
Was that the kind of information you are looking for?
Take a look at several examples of Custom MVC Model binders on my blog.

using ViewModels for POST actions in MVC elegantly

Currently I'm passing my domain objects to my views, and binding directly to them from POSTs. Everyone says this is bad, so I'm attempting to add in the ViewModel concept.
However, I can't find a way to do this very elegantly, and I'd like to know what other people's solutions are to not ending up with a very messy controller action.
the typical process for say some "add person" functionality looks like this:
make a GET request for a view representing a blank Person viewmodel
post back (in)valid data
controller binds posted data onto a person viewmodel
if binding fails, i need to do the same action as in (1) but with some data, not a blank object and errors
if the binding suceeded, i need to map the properties from the VM onto a real model
validate the model
if validation passed: save the person, commit, map the users details to a display VM and return it in a view
if validation failed, do the same actions as in (1) but with some data and errors
Doing all this in a controller action (ignoring the GET) certainly isnt SRP or DRY.
Im trying to think of a way of breaking this process up so that it does abide by SRP, is clean, modular and above all testable.
What are peoples solution to this?
I've been experimenting with custom controller-action-invokers to separate the concerns up into individual methods, smart modelbinders and just plain brute force but i havent yet come across a solution in happy with.
P.S. as it adds so much complexity, convince me why i even need to bother
I've felt the same discomfort. My only way around it has been to do the following:
Create a binder to bind and validate the view model
Create a binder to get the entity from the database (or just do this in the controller)
Call an inherited Save method in the superclass. This method takes the viewmodel and the entity that will be updated, and does all the work you listed in your steps.
The action method looks like this:
public ActionResult Whatever(TViewModel viewModel, TEntity entity)
{
return Save(viewModel, entity);
}
The base controller has a generic definition, like so:
public abstract BaseController<TEntity, TViewModel>
where TEntity : Entity
where TViewModel : ViewModel
The constructor has two dependencies, one for the entity repository and another for the model mapper, like so:
protected BaseController(IRepository<TEntity> repository, IMapper<TEntity, TViewModel> mapper)
With this in place, you can then write a protected Save method that can be called from the controller actions in the subclass, like so:
protected ActionResult Save(TViewModel viewModel, TEntity entity)
{
if (!ModelState.IsValid)
return View(viewModel);
_mapper.Map(viewModel, entity);
if (!entity.IsValid)
{
// add errors to model state
return View(viewModel);
}
try
{
_repository.Save(entity);
// either redirect with static url or add virtual method for defining redirect in subclass.
}
catch (Exception)
{
// do something here with the exception
return View(viewModel);
}
}
As far as testability, you can test the save method passing in valid/invalid view models and entities. You can test the implementation of the model mapper, the valid state of the view model, and the valid state of the entity separately.
By making the base controller generic, you can repeat this pattern for each entity/viewmodel combo in your domain, if you're creating many controllers to do the same thing.
I'm very interested to hear what others have to say about this. Great question.
The MVVM (ViewModel) pattern is definitely the one to go for, I had a similar question about POSTing back to an action a few days back - here is the link: MVVM and ModelBinders in the ASP.NET MVC Framework
The result was that you can use the Bind attribute to post back the complex type you want.
I have many good solutions in the asp.net mvc sample application which is in the download of valueinjecter (mapper that I use to map ViewModels to/from Entities, you can also map FormCollection/Request to Entities)
here's one:
public class TinyController :Controller
{
private readonly IModelBuilder<Person, PersonViewModel> modelBuilder;
public TinyController()
{
modelBuilder = new PersonModelBuilder();
}
public ActionResult Index()
{
return View(modelBuilder.BuildModel(new PersonRepository().Get()));
}
[HttpPost]
public ActionResult Index(PersonViewModel model)
{
if (!ModelState.IsValid)
return View(modelBuilder.RebuildModel(model));
var entity = modelBuilder.BuildEntity(model);
...
//save it or whatever
}
}

ASP.NET MVC - POST parameters

I have a view which contains multiple partial views, each of which is collecting information to populate different entity objects. My question is, upon the POST, how do I get a collection of objects that are populated with the right properties as a parameter to the Controller POST handler method?
so I would like something like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(List<object> objectCollection)
{
}
You got various options. Common one is to use default model binder. You just need to follow some naming (of html input elements) rules.
Advanced options are to use ActionFilters and custom model binders.
I recommend you to read this and this article.
Use FormCollection e.g...
public ActionResult Create(FormCollection frm)
{
Book book = new Book();
book.Name = frm["Name"];
// other work
return View();
}

ASP.NET MVC - Multiple models in a form and model binders

I have a form which needs to populate 2 models. Normally I use a ModelBinderAttribute on the forms post action i.e.
[Authorize]
[AcceptVerbs("POST")]
public ActionResult Add([GigBinderAttribute]Gig gig, FormCollection formCollection)
{
///Do stuff
}
In my form, the fields are named the same as the models properties...
However in this case I have 2 different models that need populating.
How do I do this? Any ideas? Is it possible?
Actually... the best way is to do this:
public ActionResult Add([GigBinderAttribute]Gig gig, [FileModelBinderAttribute]File file) {
}
You CAN use multiple attributes!
In cases like this, I tend to make a single model type to wrap up the various models involved:
class AddModel
{
public Gig GigModel {get; set;}
public OtherType OtherModel {get; set;}
}
...and bind that.
The UpdateModel or TryUpdateModel method can be used to do this. You can pass through the model, the model you wish to bind, the prefix of the items you wish to bind to that model and the form. For example if your Item model has form variables of "Item.Value" then your update model method would be:
UpdateMode(modelObject, stringPrefix, formCollection);
If you're using the entity framework, it's worth pointing out that the UpdateModel method doesn't always work under some conditions. It does work particularly well with POCOs though.

Resources