R – Trying to pass Model down to partial, how to do this

asp.net-mvcrenderpartial

My action creates a strongly typed viewdata, which is passed to my view.

In the view, I pass the Model to the render partial method.

public ActionResult Index()
{
            ViewDataForIndex vd = new ViewDataForIndex();

            vd.Users = Users.GetAll();

            return View(vd);
}


public class ViewDataForIndex: ViewData
    {
          public IList<User> Users {get;set;}

    }

now in the view:

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

<% Html.RenderPartial("~/controls/blah.ascx", ViewData.Model); %>

and in blah.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
  1. how do I access my model now?
  2. if I wanted to create a strongly typed class for my ViewUserControl, how would I do that? inherit from?

Best Answer

One: Inside the ascx:

   <%= Model.YourProperty %>

Two: Provide a type Argument to ViewUserControl:

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