R
R
Ruslan2014-12-06 10:26:58
ASP.NET
Ruslan, 2014-12-06 10:26:58

How to make User Control for Asp.Net MVC?

Hello.
Is it possible to make a user control in asp.net mvc, so that it is easy and simple to use it.
The requirements for such control are as follows:

  • To be easy to use
  • To be usable in different views
  • So that the data for the control can be substituted with various data defined by the controller, which is responsible for forming the container view for this control
  • So that the controller that processes the post request for the view in which the control is located can receive the full data model from the control, and at the same time it does not have to create an additional input parameter for it.
  • So that when the user performs actions with the control, the user returns to the page where the control is located.

Now what options do I see for solving this problem:
  • To build a controller, you need to use a service class for its logic and a helper class or a Controller-Partial View binding for rendering.
  • To pass initiating data for the control, use a separate model class, which will be formed and initialized in the view action, and in the view itself, the model will be passed to Helper
  • To pass data from the control back to the action, you can use the Controller.UpdateModel<>() method
  • In order for the control to perform some actions (for example, checking user input on the server side), you can call some method of the service class responsible for the logic, if you need to perform actions specific to the host controller, you can pass a delegate to this method to your own method or use the construction Action<> (Func<>) + lambda expression.
  • To return the user to the page on which he was previously, you can save the Url with the control and then go to it.

Something like that. There are probably some frameworks for this.
If you have experience in building "correct" controls for asp.net mvc, please share.
Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Polyankin, 2015-06-13
@ranquild

Use DisplayTemplates or EditorTemplates. It seems to be closest to what you write about.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question