You could try using the UpdateModel(StoreObject);
Perhaps the values will get set.
You could try using the UpdateModel(StoreObject);
Perhaps the values will get set.
Rename the form elements to match the objectname.propertyname, like so:
<%= Html.TextBox("SomeNewObj.Id",Model.Id);
<%= Html.TextBox("SomeNewObj.Title",Model.Title);
where SomeNewObj is the name of the parameter to the POST method:
public ActionResult SomePostMethod(int id, Store SomeNewObj)
{
//SomeNewObj.Title should be properly populated
//...
}
ASP.NET MVC will make the association to the new Store object only if the form IDs include the name of the object parameter as well as its properties.
Remember to encode! :) I left that out in this example.
Your form must include all non-nullable properties of the type, or the default model binder won't bind it. You say that you don't include the ID. Is this a non-nullable type like Guid or int? That's your problem.
There are two ways to work around this:
I note that you are using TextBoxes to bind to Boolean properties. This means that if any value is omitted or cannot be converted to Boolean, binding of the entire type will fail, because the properties are non-nullable. Using anything other than a checkbox to bind to a Boolean is a bit precarious. In general, if you design a type for model binding, you may want to make everything nullable in the case where a user not filling out one property doesn't mean that you don't want to see what they filled in for other properties.
I'd setup a CustomModelBinder for it. for a little up front work, these payoff in the long run, believe me...
public class StoreModelBinder : DefaultModelBinder
{
protected override void OnModelUpdated(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
Store store=
(Store)(bindingContext.Model ?? new Store());
// For example...
store.ID = Convert.ToInt32(controllerContext.HttpContext.Request["storeID"]);
// Set other properties...
}
}
You get reuse across controllers and actions AND your controllers don't have to worry about digesting FormCollections.
The problem was with the IoC framework I was using. Well it wasn't a problem with the framework itself, but rather my configuration of it. When using Castle Windsor for your IoC framework you must mark the containers being registered as having a lifestyle = Transient instead of the default Singleton. If you allow the default it will wreak havoc when using it as I am.
Thank you all for your help.