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 looking for recommendations for something I am confronted with regularly. For most forms in MVC I am able to use the Lighspeed model objects directly. For situations where the form has a file upload control, I run into issues that cause me to create a special view model. I'm not liking the idea of maintaining two classes that are essentially the same. Just wondering what are the suggested apporaches for this situation? Ideally, I would like to extend the Lighspeed model object to include a transient property to hold the HttpPostedFile class and migrate that over to the assoicated database types. I tried to add a transient property to my model with type object and worked except for the validation (I want to have the item validated for presence in the form). I also tried to create a partial class to extend the Lightspeed object and I was unable to get that working as well. Is this something that needs to be done in a custom model binder?
Any examples you could give me would be greatly appreciated.
Thanks |
|
|
Transient fields do participate in validation, so I'm not sure why you weren't seeing this happening. What was the issue? Did LightSpeed not validate the field? Or was it that the validation wasn't showing up in the UI? You don't say what problem you ran into when creating the partial class to extend the LightSpeed entity. Could it have been that your partial class was in the wrong namespace? Visual Studio likes to stick folder names onto namespaces, which if you don't notice it can mean that your partial is called e.g. MyProject.CustomCode.Widget instead of MyProject.Widget. You can right-click an entity and choose Refactor > Create Partial Class and the designer should take care of this for you. (That said, regardless of this specific problem, a bit of guidance on handling custom view models in general would make for a nice blog post. I'll mention it to our Web gurus!) |
|
|
Hi Ivan: Sorry for not being very specific about the errors I was seeing. I guess I was just looking for some general guidance. Here is a better description. 1) Adding a transient to the Lightspeed model of type Object. Checking the returned view model (Lightspeed object) I see the following things when not assigning a file to the upload control. The transient property is included in the ModelState which seems to suggest that it is being validated. It does not however flag an error. I'm guessing this is the case because MVC seems to populate the transient property with an array of HttpPostedFileBase objects. In this case there is only one object in the array and it is set to null. I suspect the validation is against the array being not null not the contents of the array. Not sure what the Model Binder does in this case. This is an MVC question but I am not sure why an array is returned instead of just the object. I'm guessing it has something to do with the fact that the type is Object.
2) Extending the Lighspeed object using a partial class. I did figure out the namespace issue with partial classes because the transient property wasn't visible untill that was fixed. The partial class is as follows:
public partial class LighspeedObject { [Transient] [Required] public HttpPostedFileWrapper DocumentTransient; } I was a little concerned about this approach because I couldn't define DocumentTransient as a property becuase of the [Transient] attribute. In this case the transient property was populated correctly (null) but it wasn't included in the validation even though I put the [Required] attribute on it. Also, if I do assign a file then the transient property remains null. I have a custom view model with the same property and it works fine with the form. Most of these issues are related to MVC but was wondering whyI can't dedine a normal property with the [Transient] attribute.
I suppose I could just create a simpler custom view model that contains the Lightspeed object and an additional property for the document transient. I know the default binder won't validate the nested class so I would have to create a custom one to handle that. I would prefer not to have to do that if possible.
A blog covering this topic would be a great idea. This is a common problem that most ASP.NET MVC'ers using Lighspeed, or any ORM for that matter, are faced with.
Thanks
|
|
|
1. [quote user="thehandygeek"] I suspect the validation is against the array being not null not the contents of the array.[/quote] That's correct. You could get around this with a custom validation. 2. You can define DocumentTransient as a property using old-skool property syntax: [Transient] public HttpPostedFileWrapper DocumentTransient { You can't put Transient on a property because LightSpeed doesn't think in terms of properties: it thinks in terms of fields. |
|
|
I'll give that a try Ivan, many thanks!
|
|