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
|
Seems like the Mindscape MVC hasn't been updated in a while and a couple of issues have been found depending on entity setup. Is there any plan to integrate solutions such as LuckyRat's on inherited base classes etc? I know it's not a super complex project, but I always get nervous about branching my own code on a community project. Thanks! Eric |
|
|
Hi Eric, Thanks for the heads up - had not noticed the prior post :) Ive updated the ModelHelper class with Luckyrats changes now.
Jeremy |
|
|
Great, thanks Jeremy. Just curious, is there any plan for this project? I'm going to be using lightspeed/MVC pretty intensively for the next month or so and I'm sure I'll add some enhancements along the way. Is there any plan to make this a true community project with other committers? Thanks, Eric |
|
|
Yes, we have some bits from an active project which I am keen to look at pushing into this going forward. We do however need to finish that project and then review things first, but if you have any contributions in the meantime then we would love to see them (and include them if appropriate) :)
|
|
|
Hi Jeremy, mainly I'm just trying to iron out small defects, but to be honest, since I'm so green on MVC, it's hard to tell if they are defects or my own ignorance on how to leverage MVC. Here's an example I posted to your blog. Any ideas? Do you have any examples of using this in an update situation, as opposed to the create that you have in your blog example?
|
|
|
Hi Eric :) In an update scenario I take the new object which is coming in through the request and unbind it as appropriate against the existing object from the repository. e.g. Would look something like this, but generally your Update is likely to be a bit more specific to whatever you are actually doing :) ActionResult Update(Product product, long id) { var existingProduct = _unitOfWork.FindOne<Product>(id); // validate we should be allowed to update existingProduct // unbind the required data from product into existingProduct existingProduct.UpdateFrom(product); _unitOfWork.SaveChanges(); } If your intent is to always unbind against an object, where you are passing in the Id parameter at the same time, you could update the model binder to load the required object in, however that does open up a lot of potential issues around security, so I prefer to always handle that in the action methods directly. I personally dont like those types of approaches, but if you have more of an admin form updating the object in its entirety that would be a reasonable scenario and you would presumably be guarding the action via a security filter up front. Jeremy |
|
|
Jeremy, where does the "UpdateFrom" come from? I'm guessing it's an extension method, but is it from Lightspeed or from MVC? I can't seem to find it in either namespace. Thanks, Eric |
|
|
Nevermind, I see that UpdateFrom is now defunct. |
|
|
Sorry - that was just a contrived example, but UpdateFrom being a method on your model object which handles the conditional updating of fields. I believe it may also be a MVC extension method which is what you are referring to? Generally we use more specific methods on the model to update it as required, e.g. UpdateAddressDetailsFromExistingProduct(product) etc etc. Are you looking to take a more generic approach with what you are doing? |
|
|
Jeremy, thanks so much for your help, I've got it running now. I wanted to point out an issue I experienced that somebody else blogged about. I updated the codebase with their example and this resolved my issue: http://www.nathanli.net/asp-net-mvc-and-mindscape-lightspeed-lesson-1/ In addition, in case anybody else references this article, I also wanted to note that I used your "Bind" extension method from the ModelHelper class in place of the "UpdateFrom" that is no longer available in MVC. My basic up, date logic: [AcceptVerbs(HttpVerbs.Post)]public ActionResult Edit(int id, Role role){ var existingRole = roleService.Load(id);if (ModelState.IsValid){ existingRole.Bind(role); roleService.Save(existingRole, CurrentMember); roleService.ApplyChanges(); } return View(existingRole);} |
|
|
Jeremy, do you have any advice on using the model binder if I'm using ViewModels? I'm using AutoMapper to map to simple DTO's. I'm going to be passing in a view model as opposed to my lightspeed entity, so the system kind of falls apart there. Have you tried the viewmodel approach? If so, are you still able to leverage the lightspeed model binder? Any other ideas otherwise? Thanks, Eric |
|
|
Hi Eric,
Yep, we have used ViewModels on a number of projects, but not where the inbound data also contains a LightSpeed entity. We generally only wire back specific entities say on admin screens where you are basically just posting back a form of entity data.
In general though, model binders are wired up against specific types, so in the case of the LightSpeedModelBinder it wires up against LightSpeed entity types. I suspect you would need to create a custom model binder which understands to either pass off control to the LightSpeed binder or just has that code included and will use it when unbinding a LightSpeed type. You would also need to start dealing with field prefixing similarly to the default binder I would imagine..
Sounds like an interesting one to investigate further though :)
|
|
|
In the blog post you reference, there is some code that is added in the if (!entity.IsValid) part of the BindModel method. This works, but when you do this you only end up with the properties on your entity that have errors keeping their value. I think a better solution is this: After the line:
var bindResult = bindingContext.ValueProvider[fieldName]; Add the line:
bindingContext.ModelState.SetModelValue(fieldName, bindResult);
Then when your page redisplays, all of the fields are filled in, not just the ones that had errors.
|
|