C# 如何在ASP.NET MVC4中呈现特定模型的视图?
我正在学习ASP.NETMVC4,这是我在Web开发方面的第一次体验 我正在处理一种情况。在我的一个控制器中,我将向视图返回一个C# 如何在ASP.NET MVC4中呈现特定模型的视图?,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc 4,我正在学习ASP.NETMVC4,这是我在Web开发方面的第一次体验 我正在处理一种情况。在我的一个控制器中,我将向视图返回一个IEnumerable public class TestController : Controller { public ActionResult Index() { return View(Models); } // this static method is just for demo static Lis
IEnumerable
public class TestController : Controller
{
public ActionResult Index()
{
return View(Models);
}
// this static method is just for demo
static List<BaseModel> Models()
{
...
}
}
当我想显示数据时,每个ConcrenteModel
都有自己的视图
。如下图所示
我怎样才能做到这一点?如果我不是很清楚,请告诉我
谢谢。您可以为
TestView
创建一个复合视图,然后为每个较小的视图渲染部分视图。为了实现这一点,较小视图的模型需要从传递到复合视图的模型中可用
类似这样的内容:(TestView.cshtml)
然后就有了不同的视图,比如:(View1.cshtml)
@model子视图model1
我实际上会创建一个自定义模型,其内容是您的IEnumerable
,它有自己的内置方法,用于按类型检索特定模型。这样,您可以渲染局部视图,并将正确键入的特定ConcreteModel
发送到其中。可能是这样的:
public class UltraModel
{
// I recommend using an interface rather than a base class
// to avoid potentially confusing types
public IBaseModel Models { get; set; }
public T getModelByType<T>()
{
return (T)Models.Where(x => x is T).FirstOrDefault();
}
}
公共类超模型
{
//我建议使用接口而不是基类
//避免潜在的混淆类型
公共IBaseModel模型{get;set;}
公共T getModelByType()
{
return(T)Models.Where(x=>x是T).FirstOrDefault();
}
}
然后,在全局视图中,可以使用以下选项渲染局部视图:
RenderPartial("ConcreteViewName", Model.getModelByType<ConcreteModel1>());
RenderPartial(“ConcreteViewName”,Model.getModelByType());
注意:将上述内容视为psuedo代码。它可能需要调整才能正常工作。以下是一个完整的解决方案: 模型 复合视图
@model System.Collections.Generic.List
@{
ViewBag.Title=“CompositeView”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
合成视图
@foreach(模型中的var模型)
{
RenderPartial(string.Format(“{0}”,model.GetType().Name),model);
}
具体观点
_ConcreteModel1.cshtml
@model mvcapapplication1.Models.ConcreteModel1
混凝土模型1
@模型.内容
_ConcreteModel2.cshtml
@model mvcapapplication1.Models.ConcreteModel2
混凝土模型2
@模型.内容
_ConcreteModel3.cshtml
@model mvcapapplication1.Models.ConcreteModel3
混凝土模型3
@模型.内容
控制器
public ActionResult CompositeView()
{
列表模型=新列表();
添加(新的ConcreteModel1(){Content=“这是模型1。”});
Add(newconcretemodel2(){Content=“这是模型2。”});
Add(新的ConcreteModel3(){Content=“这是Model3.”);
返回视图(模型);
}
那么您需要一个基于模型具体类型的复合UI吗?或者换句话说,您需要能够根据不同的具体类型显示未知数量的模块,比如说,
。@MichaelPerrenoud是的,基于不同的具体类型,对,我相信我已经为您提供了一个解决方案,它与您所寻找的一样动态和通用。虽然这在理论上是正确的,但如果布局要像OP所说的那样动态,则需要使用反射。OP似乎特别在寻找一种解决方案,该解决方案将使他们脱离列表,而不考虑知道列表中可能有什么。它是否消除了在操作中使用Linq联接?
@model SubViewModel1
<!-- Whatever -->
public class UltraModel
{
// I recommend using an interface rather than a base class
// to avoid potentially confusing types
public IBaseModel Models { get; set; }
public T getModelByType<T>()
{
return (T)Models.Where(x => x is T).FirstOrDefault();
}
}
RenderPartial("ConcreteViewName", Model.getModelByType<ConcreteModel1>());
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public abstract class BaseModel
{
public string Content { get; set; }
}
public class ConcreteModel1 : BaseModel { }
public class ConcreteModel2 : BaseModel { }
public class ConcreteModel3 : BaseModel { }
}
@model System.Collections.Generic.List<MvcApplication1.Models.BaseModel>
@{
ViewBag.Title = "CompositeView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
CompositeView</h2>
@foreach (var model in Model)
{
Html.RenderPartial(string.Format("_{0}", model.GetType().Name), model);
}
@model MvcApplication1.Models.ConcreteModel1
<h1>Concrete Model 1</h1>
@Model.Content
@model MvcApplication1.Models.ConcreteModel2
<h1>Concrete Model 2</h1>
@Model.Content
@model MvcApplication1.Models.ConcreteModel3
<h1>Concrete Model 3</h1>
@Model.Content
public ActionResult CompositeView()
{
List<BaseModel> model = new List<BaseModel>();
model.Add(new ConcreteModel1() { Content = "This is model 1." });
model.Add(new ConcreteModel2() { Content = "This is model 2." });
model.Add(new ConcreteModel3() { Content = "This is model 3." });
return View(model);
}