views:

28

answers:

1

Maybe this question is quite simple because I'm new to MVC2. I have a simple demo MVC project.

(1) A weak-typed view: Index.aspx

<% Html.RenderPartial("ArticalList", ViewData["AllArticals"] as List<Artical>); %>

(2) A strong-typed partial view: ArticalList.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<Artical>>" %>
<% foreach (Artical a in Model)  { %>
           <%= Html.ActionLink(a.Title, "About", new { id = a.ID })%><br />
<%} %>

(3) Here is the HomeController.cs

   public ActionResult Index()
    {
        ViewData["AllArticals"] = Artical.GetArticals();
        return View();
    }

public ActionResult ArticalList()
{
    return PartialView(Artical.GetArticals());
}

Sorry I'm using a Web-Form "angle", because if I'm using a Web-Form, when I visit Index.aspx, rendering ArticalList.ascx will call public ActionResult ArticalList(). But here I need to write Artical.GetArticals() twice in two actions. How can I put them in one?

+1  A: 

From what I understand, as a recent newbie in MVC too, is that the partial view does not use a action method in a controller. The "ArticalList" is a reference to the partial view file only and does not make another request for an action method. The partial view gets all of it's data from the view it is called from.

Html.RenderAction might be the behavior you're getting confused with.

eddiegroves
Brilliant! That's what I want! Thanks!
Danny Chen