Asp.net mvc 试图将模型传递给partial,我该怎么做?
我的操作创建了一个强类型的viewdata,并将其传递给我的视图 在视图中,我将模型传递给render partial方法Asp.net mvc 试图将模型传递给partial,我该怎么做?,asp.net-mvc,renderpartial,Asp.net Mvc,Renderpartial,我的操作创建了一个强类型的viewdata,并将其传递给我的视图 在视图中,我将模型传递给render partial方法 public ActionResult Index() { ViewDataForIndex vd = new ViewDataForIndex(); vd.Users = Users.GetAll(); return View(vd); } public class ViewDataForIn
public ActionResult Index()
{
ViewDataForIndex vd = new ViewDataForIndex();
vd.Users = Users.GetAll();
return View(vd);
}
public class ViewDataForIndex: ViewData
{
public IList<User> Users {get;set;}
}
现在在视图中:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ViewDataForIndex>" %>
<% Html.RenderPartial("~/controls/blah.ascx", ViewData.Model); %>
在blah.ascx中:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
现在如何访问我的模型?
如果我想为我的ViewUserControl创建一个强类型类,我该怎么做?继承自?
一:在ascx内:
<%= Model.YourProperty %>
第二:为ViewUserControl提供类型参数:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
我更喜欢@jfar的第二种方法,因为如果您决定将更复杂的模型传递给视图,它允许更容易的修改 因此,可以传递具有多个属性和/或多个子对象的类
如果您现在从对象继承,那么您所需要做的就是从复杂对象继承并更改一段代码,以及添加新属性,并完成操作。更具体地说,jfar的答案是:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ViewDataForIndex>" %>
如果父页面具有ViewDataForIndex类型的模型,则使用相同的ViewData.model调用子页面。该模型还将传递ViewDataForIndex类型的对象