This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi I'm trying to get model binding to work with Lightspeed on MVC3. I have a view "Create" (with a view model) which posts to itself. ie. I want to insert a new entity in the [HttpPost] Create action in the controller. My form has a bunch of text inputs created with @Html.TextBoxFor(model => model.theProduct.Title) My Create action looks like this: [HttpPost] public ActionResult Create(Product theProduct) { try { UnitOfWork.Add(theProduct); UnitOfWork.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } What is the correct syntax for doing this? At the moment the entity passed to the Create action has null values on all properties. I've also tried passing the view model to the action but viewmodel.product properties are still null. Thanks for advice! |
|
|
Are you using the default model binder or the LightSpeed entity binder? (see http://www.mindscapehq.com/documentation/lightspeed/Building-Web-Applications-/Building-ASPNET-MVC-Applications for details about this).
|
|
|
I have "LightSpeedEntityModelBinder.Register(typeof(MyModelUnitOfWork).Assembly);" in Application_Start so I assume that means I'm using the LightSpeed binder - unless some other config is required? Thanks. |
|
|
No that should be all you need. How are the input fields being named on the form? Do they match the property names on your model?
|
|
|
For example the "Title" property's input field is rendered as: <input id="theProduct_Title" type="text" value="" name="theProduct.Title"> Does the name have to be just "Title" as opposed to "theProduct.Title" for it to work? Perhaps the problem is related to the fact I'm using a view model that contains the product and product categories (as opposed to just a product):
public class ProductsCreateViewModel
{
public Product theProduct { get; set; }
public List |
|
|
Yes that will certainly be a problem as the binder expects the field name to match the property name for the entity being unbound.
|
|
|
Ok, so what's the way to handle that scenario where the view has multiple entities in it causing field names to be prefixed with the entity name? |
|
|
You will either need to revert to using the default MVC model binder which does handle this type of approach for unbinding from the form already and then manually check if the entity is valid and assign any errors into the ModelState collection if not, or alternatively create a custom implementation based the LightSpeed binder to handle this (we can supply the source code for this if this is the route you want to go down). I would suggest looking at the first approach since this is likely to be the least involved.
|
|