Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC: How to encapsulate logic in MVC ascx partial view with the same efficiency of webforms ascx controls?
    primarykey
    data
    text
    <p>I have to build a few ascx partial views in my MVC applications to encapsulate re-usable functionalities as well as archive SOR and SOC. However, I encountered challenge how to encapsulate the business logic that drives MVC ascx views. </p> <p>In webforms, the code behind of an ASCX control can handle a button click event and browser still shows the same page with only one postback. How can I archive the same thing in MVC? When ~/Address/Edit/2 includes an ascx partial view with a button on it that calls another MVC action - let’s say ~/ShareController/CommonAction – what is the best way for the CommonAction to return to the very same view that includes the ascx file?</p> <p>Perhaps I am missing a common pattern in MVC web development?</p> <p>Thank you,</p> <p>Max</p> <p>Update: Here is more description about the pattern I am looking for:</p> <p>The partial view supposed to be an address editor. It can validate the address, shows validation errors and save the information back to the database.</p> <p>The address editor is going to sit on many pages with different controller/actions. </p> <p>In classic webform, an ascx control can perfectly interact with the user on its own through many postbacks without changing the url dor disturbing the overal page process. What is the right pattern for the similar scenario in MVC?</p> <p>Update 2:</p> <p>Not sure this has anything to do with the statefullness of webform. Totally different feature. This is about a controller call another sub-controller (or child controller) to handle a partial view. See my example at the end of this comment.</p> <p>I just want to achieve the same level of encapsulation and separation of concern. If MVC framework doesn’t have that, it is a serious weakness. </p> <p>In MVC, how could we have a “thing” (or partial view for lack of better alternative) that is able to handles the presentation as well as the logic and reside within another parent page without any interfere?</p> <p>To my understanding, Partial views in MVC are just presentation that cannot have a dedicated controller without interfere with the main controller. I hope I have got it wrong</p> <p>To give you another example, consider this page that you are reading “stackoverflow.com/questions/{id}” . It includes an “Add Comment” component. The add comment might do server side validation to prevent invalid comments. How could you create an end-to-end “Add Comment” component that can reside on any page and handles necessary controller logic? The only way I can think of is to modify the controller of host page to be able to give user validation messages and ask them to correct the errors through post back. That means you add the component to 10 different pages, you have to modify 10 different controllers. That kills “DRY”</p> <p>I DO HOPE THAT I AM WRONG! because I love MVC sofar.</p> <p><strong><em>Conclusion:</em></strong></p> <p>I marked Kenny’s reply as the answer; however, I would consider this as a serious weakness of MVC framework from the encapsulation, re-usability and DRY principal perspective. </p> <p>My opinion has nothing to do with statefullness of webforms.</p> <p>Please allow me to explain:</p> <p>In webforms, You can 100% encapsulate a complex functionality in an ascx control and of course related classlib DLLs. One (1) developer can spend weeks developing it. Once his work is done, no one needs to know anything about it and just use it. The user control can be reused in many pages without bothering other “developer user”s with the detail activities that takes place within the ascx component.</p> <p>In contrast you never can achieve the same with ASP.NET MVC where the ascx partial needs to interact with the user. Once the ascx needs to interact, it needs it’s own controller and the controller needs to preserve the ModelState. </p> <p>Jeremy Skinner introduced the [ModelStateToTempData] in MVCContrib: <a href="http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/" rel="nofollow noreferrer">http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/</a> However, this is just a patch.</p> <p>A good framework should allow developer to 100% encapsulate their work – in a control, partial, ascx or whatever – and had it to the rest of the team so they can “just use it” without knowing any data. Winform perfectly delivers this in many complex scenarios. </p> <p>Any though welcome.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload