0

I have experience in writing code... but new to developing web applications.

I am in process of choosing a framework for my project. Based on my initial research almost everything is very VIEW centric where after all business logic has been executed a model is populated with data and passed on to the VIEW.

So the lowest level of granularity is the VIEW.

But I wonder what is the right technology to use if I wanted to develop re-usable widgets or controls. and then reuse them across multiple VIEWS.

i would prefer if the controls are in JavaScript and then they can easily be reused across pages. (so no asp.net server controls or web forms).

So if I were to select ASP.NET MVC 4 ... does it have anything to help me write code in reusable widgets... or will it simply ask me to write the VIEWS which work with the data provided by the model?

Sorry if this is newbie question

to eloborate on what I mean by a widget.

Suppose I am writing a discussion forum web appliction. I want to write a widget called post which has 3 views. View 1 is edit mode. View 2 is summary mode. view 3 is Detail mode.

I throw in this widget in a page called QuestionStream and in this page the widget appears in summary view. I perform data binding so that i get a list of questions.

I throw in this widget in a page called ThreadView and in this page the widget appear in detailed view. I perform data binding and I get all the details of the question.

I throw in this widget in a page called NewQuestion and in this page the widget appears in edit view.

So its a self contained control... but is reused in multiple places in different modes (so to speak).

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373

2 Answers2

1

Here is summary list on how to create reusable components/pages/widgets in MVC. There are many more ways, these are just examples to give you starting point on what to look for or how to make one.

1. Partial Views

  • Typically used for breaking down large views, into smaller views then combined at runtime
  • View-centric approach, the controller can choose a View to generate.
  • Tutorial

2. Render Action / Render Partial

  • Invokes an child ActionMethod directly in the view page as inline expression.
  • Useful for partial views that requires a business logic.

3. Custom DisplayFor/EditorFor Templates

  • Model-centric, which means they take model metadata (Attributes/Annotations).
  • Cool tutorial

4. Custom HTML Helpers

  • "Commonly used to generate boilerplate HTML" (LINK)

The following link shows how to create page "widgets" in MVC. The author doesn't mention "widgets", but it is the same as you are looking for.

http://www.mikesdotnetting.com/Article/105/ASP.NET-MVC-Partial-Views-and-Strongly-Typed-Custom-ViewModels

Based on the tutorial mentioned, below is the image on what it should look like (notice the "Most Popular" section, that widget is completely reusable across all pages)

enter image description here

Community
  • 1
  • 1
Yorro
  • 11,445
  • 4
  • 37
  • 47
0

ASP.NET MVC 4 has the ability to write methods that are re-usable. The View is expressed in mark-up (typically) and will call methods from the Controller to populate the View.

To be honest though, even though you're asking a very simple question, I think what you're trying to get at is the re-usability. All the technologies you listed: server controls, web forms and mvc4 have that capability. It all depends what you want to do.

Thomas Taylor
  • 555
  • 4
  • 12
  • I did not find anything in ASP.NET MVC which allows me to write a self contained widget. which can be reused accross pages. Can you point me to a tutorial or code which shows how to write a widget and then reuse accross multiple pages. – Knows Not Much Feb 19 '14 at 19:43
  • I really don't know what you mean by the term "widget". Web parts might be a close thing ( http://msdn.microsoft.com/en-us/library/e0s9t4ck.aspx ) but I'm still not sure what you're really asking. Most .NET code can be re-used across pages and systems. Console applications, web applications, whatever. – Thomas Taylor Feb 19 '14 at 19:45
  • I don't want to use web forms. I know they have server controls. but I don't want viewstate and I want client side focus. Someone pointed out helper methods to me http://forums.asp.net/t/1969367.aspx?Coming+back+to+web+development+after+a+long+time but then they are not like self contained widgets. – Knows Not Much Feb 19 '14 at 19:46
  • I still don't understand what you mean by self-contained widget. Could you give me an example? – Thomas Taylor Feb 19 '14 at 19:47
  • Web Parts can be good. do they work with MVC? I thought they were similar to the server controls which don't work in the MVC model. – Knows Not Much Feb 19 '14 at 19:47
  • edited the question and added more details of what I mean by widget. – Knows Not Much Feb 19 '14 at 19:57