开发者

Help me understanding MVC.NET

I am used to working with ASP.NET FORMS and I am having a hard time understanding MVC.NET.

If I in WebForms would create a profile page for a user, it would consist of an aspx page with several usercontrols add开发者_运维技巧ed on the page, which all does something differently. One control might show the latest posts by the user, another the friends connected to the user etc.

But how would this be done in MVC.NET?

Am I supposed to create several views for each user control, or am I supposed to create one huge model which contains all the data I need on the page and then feed it to the view?


For me, if a the partial view (.ascx or usercontrol in asp.net forms) has a lot of dynamic contents on it, I will make it a strongly typed view. Which means it has its own Model. Depending on its usability. But I strongly advise make each Views its corresponding Model.

For example, in this situation, you have a partial view that has a Model of Address

Partial View

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Address>" %>

Address Name: <%=Model.AddressName %>
Address Location: <%=Model.AddressLocation %>

Model

public class Address
{
     public string AddressName {get;set;}
     public string AddressLocation {get;set;}
}

And I want to use this Partial View of mine depending on the page that is using. Could be the Login User or the Friends User. What I would do is just call its Corresponding Action in a Controller.

Say I put it in the CommonController

public class CommonController : Controller
{
     ModelRepository modelRepository = new ModelRepository();

     public PartialViewResult AddressPartialView(int id)
     {
          var address = modelRepository.GetAddress(id);
          return View("AddressPartialView", address);
     }
}

In this case I can call this Partial View in any Part of any page by

<%= Html.Action("AddressPartialView", "Common", new { id = Model.AddressId })%>

assuming my View that I was calling the Partial View has a Model of User with a property of AddressId. This way, I can use and reuse my Partial View in 1 single page, each got different content.


It's a different way of thinking, but more aligned to separation of concerns and once you've started working with it, you'll never want to move back to asp.net webforms :) (Just my opinion).

Let's take your example 1 Webform (aspx) page would contain a set of user controls and you either have the logic sitting in the code behind, be it in the user controls or wherever.

With asp.net mvc you have the following. a Model, which you can think of as your Data Access Layer. What I try to do in my projects is create a Model that I will use to interact with the View.

a Controller, which delegates which view is to be presented.

a View, should be as dumb as possible and should only contain presentation logic. You could also create partial views, which you can think of as your controls

Let's take Stack Overflow as an example. Questions can be a View and your tags and advertisements on the side, can be partial views. The Model can have a List and a bunch of CRUD (Create, Read, Update, Delete) methods.

That's about it. You can read some more and get a better understanding from http://www.asp.net/mvc


Creating user controls is still possible in ASP.NET MVC (these are also called partial views). Difference with the WebForm approach is that these user controls get their data from their parent view. For instance:

A view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"   
Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    ...
    <% Html.RenderPartial("MyUserControl", Model.UserControlViewModel); %>
    ...
</asp:Content>

The user control (this one should be named MyUserControl.ascx):

<%@ Control Language="C#" 
Inherits="System.Web.Mvc.ViewUserControl<MyUserControlViewModel>" %>
...

MyViewModel contains all data for both the view and its user control:

public class MyViewModel
{
    public MyUserControlViewModel UserControlViewModel {get; set;}
}

I found the tutorial on Scott Gu's blog a good starting point.


Choosing a presentation pattern for a new or enterprise web development on the Microsoft platform is a daunting task, in my opinion there are only three; View Model, Model-View-Presenter (MVP) or ASP.NET MVC (a Model2 derivative).

Thus article will help you understand that to choose when ASP.NET MVC Patterns

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜