Updating Parent/Child Records with model binders in ASP.Net MVC

前端 未结 3 1734
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 15:38

I have modified the Nerd Dinner application to allow editing of child records by adding the following code to the DinnerForm.ascx

  

<%int i

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 16:17

    Your question is whether updating parent/child records in one shot by calling UpdateModel(Parent); possible with the current model binders?

    Yes, this is possible. However, it is not possible (without some minor workarounds) if you are using Linq-to-Sql and your child objects are exposed via EntitySet instead of something like IList. I believe this will be (has been?) addressed in the next version of MVC due out with ASP.NET 4.0. For the time being, though, the MVC default model binder does not seem to understand how to work with EntitySet.

    Please see this other answer I wrote on a related question some time ago, with details about how I am now dealing with (working around) this situation in certain simple cases. In doing this, I've had to stop worrying so much about how "ideal" this solution is from a DDD/OOP perspective, as a trade-off to simply getting MVC and LTS to play together nicely with minimal effort.

    Note, I will admit that James S's solution is probably more "pure," but in order to do this in "one shot" as you ask, you'll need to either wait for ASP.NET 4.0, or use a workaround similar to what you'll find in my other post.

    Good luck!

提交回复
热议问题