单一视图中的多个模型(C#MVC3)
我正在使用C#和MVC3 我有一个页面,例如学生列表,它显示学生列表,这是数据库驱动的。同时,我的菜单是数据库驱动的,因此我还必须将其发送到视图单一视图中的多个模型(C#MVC3),c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我正在使用C#和MVC3 我有一个页面,例如学生列表,它显示学生列表,这是数据库驱动的。同时,我的菜单是数据库驱动的,因此我还必须将其发送到视图 如何将两个模型发送到一个视图?您可以创建一个视图模型,它是视图的表示,而不是业务模型 public class StudentPage { public IEnumerable<Student> Students { get; set; } public Menu Menu { get; set; } } 我假设菜单在你的页
如何将两个模型发送到一个视图?您可以创建一个视图模型,它是视图的表示,而不是业务模型
public class StudentPage {
public IEnumerable<Student> Students { get; set; }
public Menu Menu { get; set; }
}
我假设菜单在你的页面上是一个重复出现的功能,所以你可能想像这样把它分解一下
public class BasePage {
public Menu Menu { get; set; }
}
public class StudentPage : BasePage {
public IEnumerable<Student> Students { get; set; }
}
公共类基页{
公共菜单{get;set;}
}
公共班级学生页面:基本页面{
公共IEnumerable学生{get;set;}
}
您还可以创建一个基本控制器,该控制器具有跨多个控制器重复使用的
GetMenu()
功能。您应该始终为视图创建单独的视图模型。应该有一个从视图到域模型的抽象。在演示/教程中,他们通过简单地将视图强输入到域模型中来展示这一切,但这不是一个好的策略。视图不应依赖于业务对象
您应该为当前场景以及所有其他视图实施David Glenn建议的解决方案,即使需要将域模型映射到另一个视图模型类
编辑:
如果您有一个top Menu>TopMenu.aspx
在它里面有多个局部视图>StudentMenu.ascx
,ResultMenu.ascx
您将为Top Menu>TopMenuViewModel.cs创建视图模型
您还将为局部视图创建视图模型>StudentMenuViewModel
,ResultMenuViewModel
等
并且您的TopMenuViewModel将同时具有这两个>
class TopMenuViewModel
{
//all the stuff required in TopMenu.aspx
StudentMenuViewModel studentvm;
ResultMenuViewModel resultvm;
}
在TopMenu.aspx
中,渲染局部时,将传递相关的视图模型>
Html.RenderPartial('StudentView',Model.studentvm)
希望它有意义
注意:下面的类应该在.net 3.5及更低版本中使用,因为.net 4作为类似的类引入,称为Tuple
,应该使用
MultiObject
和MultiList
以下是我编写此类控制器操作和视图的方式:
public ActionResult MultiModel()
{
MultiList<User, Company> result = MultiList.New(
this.repository.GetUsers(),
this.repository.GetCompanies()
);
return View(result);
}
公共行动结果多模式()
{
多列表结果=多列表。新建(
this.repository.GetUsers(),
this.repository.getcompanys()
);
返回视图(结果);
}
我的观点是:
ViewPage<MultiList<User, Company>>
ViewPage
我正在使用这个可重用的便利类:
#region MultiObject static helper class
/// <summary>
/// Provides static methods for creating multi objects with type inference.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
public static class MultiObject
{
/// <summary>
/// Creates a new <see cref="MultiObject{T1, T2}"/> object instance.
/// </summary>
/// <typeparam name="T1">The type of the first object.</typeparam>
/// <typeparam name="T2">The type of the second object.</typeparam>
/// <param name="first"><typeparamref name="T1"/> object instance.</param>
/// <param name="second"><typeparamref name="T2"/> object instance.</param>
/// <returns>
/// Returns a <see cref="MultiObject{T1, T2}"/> of <typeparamref name="T1"/> and <typeparamref name="T2"/> object instances.
/// </returns>
public static MultiObject<T1, T2> New<T1, T2>(T1 first, T2 second)
{
return new MultiObject<T1, T2>(first, second);
}
/// <summary>
/// Creates a new <see cref="MultiObject{T1, T2, T3}"/> object instance.
/// </summary>
/// <typeparam name="T1">The type of the first object.</typeparam>
/// <typeparam name="T2">The type of the second object.</typeparam>
/// <typeparam name="T3">The type of the third object.</typeparam>
/// <param name="first"><typeparamref name="T1"/> object instance.</param>
/// <param name="second"><typeparamref name="T2"/> object instance.</param>
/// <param name="third"><typeparamref name="T3"/> object instance.</param>
/// <returns>
/// Returns a <see cref="MultiObject{T1, T2, T3}"/> of <typeparamref name="T1"/>, <typeparamref name="T2"/> and <typeparamref name="T3"/> objects instances.
/// </returns>
public static MultiObject<T1, T2, T3> New<T1, T2, T3>(T1 first, T2 second, T3 third)
{
return new MultiObject<T1, T2, T3>(first, second, third);
}
}
#endregion
#region MultiObject<T1, T2>
/// <summary>
/// Represents a 2-multi object, or pair.
/// </summary>
/// <typeparam name="T1">The type of the multi object's first component.</typeparam>
/// <typeparam name="T2">The type of the multi object's second component.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
public class MultiObject<T1, T2>
{
/// <summary>
/// Gets or sets the value of the first multi object component.
/// </summary>
/// <value>The first.</value>
public T1 First { get; set; }
/// <summary>
/// Gets or sets the value of the second multi object component.
/// </summary>
/// <value>The second multi object component value.</value>
public T2 Second { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MultiObject{T1, T2}"/> class.
/// </summary>
/// <param name="first">Multi object's first component value.</param>
/// <param name="second">Multi object's second component value.</param>
public MultiObject(T1 first, T2 second)
{
this.First = first;
this.Second = second;
}
}
#endregion
#region MultiObject<T1, T2, T3>
/// <summary>
/// Creates a new 3-multi object, or triple.
/// </summary>
/// <typeparam name="T1">The value of the first component of the multi object.</typeparam>
/// <typeparam name="T2">The value of the second component of the multi object.</typeparam>
/// <typeparam name="T3">The value of the third component of the multi object.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
[SuppressMessage("Microsoft.Design", "CA1005:AvoidExcessiveParametersOnGenericTypes")]
public class MultiObject<T1, T2, T3> : MultiObject<T1, T2>
{
/// <summary>
/// Gets or sets the value of the third multi object component.
/// </summary>
/// <value>The third multi object component value.</value>
public T3 Third { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MultiObject{T1, T2, T3}"/> class.
/// </summary>
/// <param name="first">Multi object's first component value.</param>
/// <param name="second">Multi object's second component value.</param>
/// <param name="third">Multi object's third component value.</param>
public MultiObject(T1 first, T2 second, T3 third)
: base(first, second)
{
this.Third = third;
}
}
#endregion
#区域多对象静态辅助对象类
///
///提供使用类型推断创建多个对象的静态方法。
///
[SuppressMessage(“Microsoft.Naming”、“CA1704:Identifiers应正确拼写”,MessageId=“Multi”)]
公共静态类多对象
{
///
///创建一个新的对象实例。
///
///第一个对象的类型。
///第二个对象的类型。
///对象实例。
///对象实例。
///
///返回和对象实例的集合。
///
公共静态多对象新建(T1第一,T2第二)
{
返回新的多对象(第一个、第二个);
}
///
///创建一个新的对象实例。
///
///第一个对象的类型。
///第二个对象的类型。
///第三个对象的类型。
///对象实例。
///对象实例。
///对象实例。
///
///返回、和对象实例的集合。
///
公共静态多对象新建(T1第一,T2第二,T3第三)
{
返回新的多对象(第一个、第二个、第三个);
}
}
#端区
#区域多目标
///
///表示2-multi对象或对象对。
///
///多对象的第一个组件的类型。
///多对象的第二个组件的类型。
[SuppressMessage(“Microsoft.Naming”、“CA1704:Identifiers应正确拼写”,MessageId=“Multi”)]
公共类多对象
{
///
///获取或设置第一个多对象组件的值。
///
///第一个。
公共T1第一个{get;set;}
///
///获取或设置第二个多对象组件的值。
///
///第二个多对象组件值。
公共T2秒{get;set;}
///
///初始化类的新实例。
///
///多对象的第一个组件值。
///多对象的第二个组件值。
公共多对象(T1第一,T2第二)
{
这个。第一=第一;
这个。秒=秒;
}
}
#端区
#区域多目标
///
///创建一个新的3重对象或三重对象。
///
///多对象的第一个组件的值。
///多对象的第二个组件的值。
///多对象的第三个组件的值。
[SuppressMessage(“Microsoft.Naming”、“CA1704:Identifiers应正确拼写”,MessageId=“Multi”)]
[SuppressMessage(“Microsoft.Design”、“CA1005:AvoidExcessiveParametersSongenericTypes”)]
公共类多对象:多对象
{
///
///获取或设置第三个多对象组件的值。
///
///第三个多对象组件值。
公共T3第三{get;set;}
///
///初始化类的新实例。
///
///多对象的第一个组件值。
///多对象的第二个组件值。
///多对象的第三个组件值。
公共多对象(T1第一,T2第二,T3第三)
:底座(第一、第二)
{
这个。第三=第三;
}
}
#端区
任何时候我必须通过多个列表
#region MultiObject static helper class
/// <summary>
/// Provides static methods for creating multi objects with type inference.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
public static class MultiList
{
/// <summary>
/// Creates a new <see cref="MultiObject{T1, T2}"/> object instance.
/// </summary>
/// <typeparam name="T1">The type of the first object.</typeparam>
/// <typeparam name="T2">The type of the second object.</typeparam>
/// <param name="first"><typeparamref name="T1"/> object instance.</param>
/// <param name="second"><typeparamref name="T2"/> object instance.</param>
/// <returns>
/// Returns a <see cref="MultiObject{T1, T2}"/> of <typeparamref name="T1"/> and <typeparamref name="T2"/> object instances.
/// </returns>
public static MultiList<T1, T2> New<T1, T2>(IList<T1> first, IList<T2> second)
{
return new MultiList<T1, T2>(first, second);
}
/// <summary>
/// Creates a new <see cref="MultiObject{T1, T2, T3}"/> object instance.
/// </summary>
/// <typeparam name="T1">The type of the first object.</typeparam>
/// <typeparam name="T2">The type of the second object.</typeparam>
/// <typeparam name="T3">The type of the third object.</typeparam>
/// <param name="first"><typeparamref name="T1"/> object instance.</param>
/// <param name="second"><typeparamref name="T2"/> object instance.</param>
/// <param name="third"><typeparamref name="T3"/> object instance.</param>
/// <returns>
/// Returns a <see cref="MultiObject{T1, T2, T3}"/> of <typeparamref name="T1"/>, <typeparamref name="T2"/> and <typeparamref name="T3"/> objects instances.
/// </returns>
public static MultiList<T1, T2, T3> New<T1, T2, T3>(IList<T1> first, IList<T2> second, IList<T3> third)
{
return new MultiList<T1, T2, T3>(first, second, third);
}
}
#endregion
#region MultiList<T1, T2>
/// <summary>
/// Represents a 2-multi object, or pair.
/// </summary>
/// <typeparam name="T1">The type of the multi object's first component.</typeparam>
/// <typeparam name="T2">The type of the multi object's second component.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
public class MultiList<T1, T2> : MultiObject<IList<T1>, IList<T2>>
{
/// <summary>
/// Initializes a new instance of the <see cref="MultiList<T1, T2>"/> class.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
public MultiList(IList<T1> first, IList<T2> second) : base(first, second) { }
}
#endregion
#region MultiList<T1, T2, T3>
/// <summary>
/// Creates a new 3-multi object, or triple.
/// </summary>
/// <typeparam name="T1">The value of the first component of the multi object.</typeparam>
/// <typeparam name="T2">The value of the second component of the multi object.</typeparam>
/// <typeparam name="T3">The value of the third component of the multi object.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
[SuppressMessage("Microsoft.Design", "CA1005:AvoidExcessiveParametersOnGenericTypes")]
public class MultiList<T1, T2, T3> : MultiObject<IList<T1>, IList<T2>, IList<T3>>
{
/// <summary>
/// Initializes a new instance of the <see cref="MultiList<T1, T2, T3>"/> class.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
/// <param name="third">The third.</param>
public MultiList(IList<T1> first, IList<T2> second, IList<T3> third) : base(first, second, third) { }
}
#endregion
#区域多对象静态辅助对象类
///
///提供使用类型推断创建多个对象的静态方法。
///
[SuppressMessage(“Microsoft.Naming”、“CA1704:Identifiers应正确拼写”,MessageId=“Multi”)]
公共静态类多重列表
{
///
///创建一个新的对象实例
#region MultiObject static helper class
/// <summary>
/// Provides static methods for creating multi objects with type inference.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
public static class MultiList
{
/// <summary>
/// Creates a new <see cref="MultiObject{T1, T2}"/> object instance.
/// </summary>
/// <typeparam name="T1">The type of the first object.</typeparam>
/// <typeparam name="T2">The type of the second object.</typeparam>
/// <param name="first"><typeparamref name="T1"/> object instance.</param>
/// <param name="second"><typeparamref name="T2"/> object instance.</param>
/// <returns>
/// Returns a <see cref="MultiObject{T1, T2}"/> of <typeparamref name="T1"/> and <typeparamref name="T2"/> object instances.
/// </returns>
public static MultiList<T1, T2> New<T1, T2>(IList<T1> first, IList<T2> second)
{
return new MultiList<T1, T2>(first, second);
}
/// <summary>
/// Creates a new <see cref="MultiObject{T1, T2, T3}"/> object instance.
/// </summary>
/// <typeparam name="T1">The type of the first object.</typeparam>
/// <typeparam name="T2">The type of the second object.</typeparam>
/// <typeparam name="T3">The type of the third object.</typeparam>
/// <param name="first"><typeparamref name="T1"/> object instance.</param>
/// <param name="second"><typeparamref name="T2"/> object instance.</param>
/// <param name="third"><typeparamref name="T3"/> object instance.</param>
/// <returns>
/// Returns a <see cref="MultiObject{T1, T2, T3}"/> of <typeparamref name="T1"/>, <typeparamref name="T2"/> and <typeparamref name="T3"/> objects instances.
/// </returns>
public static MultiList<T1, T2, T3> New<T1, T2, T3>(IList<T1> first, IList<T2> second, IList<T3> third)
{
return new MultiList<T1, T2, T3>(first, second, third);
}
}
#endregion
#region MultiList<T1, T2>
/// <summary>
/// Represents a 2-multi object, or pair.
/// </summary>
/// <typeparam name="T1">The type of the multi object's first component.</typeparam>
/// <typeparam name="T2">The type of the multi object's second component.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
public class MultiList<T1, T2> : MultiObject<IList<T1>, IList<T2>>
{
/// <summary>
/// Initializes a new instance of the <see cref="MultiList<T1, T2>"/> class.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
public MultiList(IList<T1> first, IList<T2> second) : base(first, second) { }
}
#endregion
#region MultiList<T1, T2, T3>
/// <summary>
/// Creates a new 3-multi object, or triple.
/// </summary>
/// <typeparam name="T1">The value of the first component of the multi object.</typeparam>
/// <typeparam name="T2">The value of the second component of the multi object.</typeparam>
/// <typeparam name="T3">The value of the third component of the multi object.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Multi")]
[SuppressMessage("Microsoft.Design", "CA1005:AvoidExcessiveParametersOnGenericTypes")]
public class MultiList<T1, T2, T3> : MultiObject<IList<T1>, IList<T2>, IList<T3>>
{
/// <summary>
/// Initializes a new instance of the <see cref="MultiList<T1, T2, T3>"/> class.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
/// <param name="third">The third.</param>
public MultiList(IList<T1> first, IList<T2> second, IList<T3> third) : base(first, second, third) { }
}
#endregion
public ActionResult Index() {
ViewBag.TopMenu = TopMenu();
ViewBag.Student = Student();
return View();
}
@{
ViewBag.Title = "Index_ViewBag";
}
<h2>Index View Bag</h2>
<table>
<tr>
@foreach (var menu in ViewBag.TopMenu)
{
<td>
<a href="@menu.URL">@menu.Name</a>
</td>
}
</tr>
</table>
<p>
<ul>
@foreach (var student in ViewBag.Student)
{
<li>
<a href="@student.URL">@student.Name</a>
</li>
}
</ul>
</p>
@Html.RenderAction("Menu");
public class HomeController : Controller
{
public ActionResult Index()
{
dynamic viewmodel = new ExpandoObject();
viewmodel.Students = MyStudent();
viewmodel.MenuItems = MyMenuItems();
return View(mymodel);
}
}
@model dynamic
@foreach (Student student in Model.Students)
}
<h1>@student.Name</h1>
}
@foreach (MenuItem menuItem in Model.MenuItems)
{
<h1>@menuItem.menuname</h1>
}