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
|
How to elegantly wire up LS to MVC3 (or thats what I'm trying to do!) http://stackoverflow.com/questions/5366504/mvc3-validation-with-lightspeed |
|
|
If the issue is with validation errors rendering as a single string on one line, this is because that line is the output of the Errors.ToString() call rather than an enumeration of each of the errors. This is where you will want to use the custom model binder to handle the error output accordingly, if you are happy with your current model binder then just extend it and overwrite the error handling behavior in your derived instance e.g. implement BindModel, call base.BindModel() then check: if (!entity.IsValid) Otherwise have a look at the example model binder (attached).
Jeremy
|
|
|
Hi Am using the attached modelBinder and have wired up in global.asax
protected void Application_Start() { AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes);
//EntityModelBinder.Register(typeof(FilmFestival.Model.UnitOfWork).Assembly); EntityModelBinder.Register(typeof(ValidationLS.Models.LightSpeedModel1UnitOfWork).Assembly); }
However when I try and bind an int value it catches an exception as shown in the screenshots. Question: What I am doing wrong?
|
|
|
Yep, thats because the model binder is only dealing with strings since bindResult.AttemptedValue is a string in the case above. If you only need to leverage the error handle semantics, then fall back on the DefaultModelBinder as a base since it has smarts around handling the expected types built in, or if you want to extend that sample to handle it then you would need to add in a type conversion to force bindResult.AttemptedValue into the type which is dictated by the valueModel. e.g. Type fieldType = (Type)valueModel.GetType().GetProperty("FieldType", BindingFlags.Public | BindingFlags.Instance).GetValue(valueModel, null); ... targetProperty.GetSetMethod().Invoke(
Note: I havnt really tested this, so there are likely some cases which you would need to look further into, e.g. nullables probably are not handled for example, but hopefully that gives you a start if you are interested in modifying it.
Jeremy |
|
|
Hi Jeremy You are right I'm trying to get the validation working, and don't really mind about DataBinding. I want the LS Attributes eg [ValidatePresence] to work like the DataAnnotation attributes eg [Required] then MVC3 will do nice things :-) I'm only interested in server side validation too. Thanks again for this great support. Dave |
|
|
Hi Jeremy Could you show me how to extend the current ModelBinder please. I've tried this (which doesn't compile) wire up in global.asax ModelBinders.Binders.Add(typeof(bool), new BBinder()); then new class:
public class BBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (!entity.IsValid) { foreach (var error in entity.Errors) { if (error.ErrorMessage.EndsWith("is invalid")) continue; bindingContext.ModelState.AddModelError(error.PropertyName ?? "Custom", error.ErrorMessage); } }
return base.BindModel(controllerContext, bindingContext); } }
Cheers Dave |
|
|
Try this: public class EntityModelBinder2 : DefaultModelBinder and in Global.asax register using: EntityModelBinder2.Register(typeof(MyEntity).Assembly);
The Register call sets up the model binder to be used for each entity type in your model assembly so modify as required.
Jeremy |
|
|
Hi Jeremy Awesome. When you say EntityModelBinder2.Register(typeof(MyEntity).Assembly); I plugged in: EntityModelBinder2.Register(typeof(Competitor).Assembly); then the rest of my entities are being registered too. This is great and what I want...
Question: Am I doing this right?
Thanks again |
|
|
Yes thats right :) If you have a look at the Register code it is just walking the types in that assembly to look for anything that inherits from the Entity base class and registers the model binder against that specific type.
Jeremy |
|
|
Great stuff. Cheers. |
|