How to visualize the representation of certain models in ASP.NET MVC4?

I am learning ASP.NET MVC4, this is in my first experience with website development.

I am dealing with a situation. In one of my controllers I return IEnumerable<BaseClass> into view.

 public class TestController : Controller { public ActionResult Index() { return View(Models); } // this static method is just for demo static List<BaseModel> Models() { ... } } 

Imagine that we have 6 specific classes in BaseModels.

 public class ConcreteModel1 : BaseModel { .. } // Must show view1 public class ConcreteModel2 : BaseModel { .. } // Must show view2 public class ConcreteModel3 : BaseModel { .. } // and so on.. public class ConcreteModel4 : BaseModel { .. } public class ConcreteModel5 : BaseModel { .. } public class ConcreteModel6 : BaseModel { .. } 

When I want to display data, each ConcrenteModel has its own View . As shown in the picture below.

enter image description here

How can i do this? If I was not very clear, please let me know Thanks.

+4
source share
3 answers

Here is the complete solution:

Models

 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 { } } 

composite view

 @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.); @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); } 

specific presentation

_ConcreteModel1.cshtml

 @model MvcApplication1.Models.ConcreteModel1 <h1>Concrete Model 1</h1> @Model.Content 

_ConcreteModel2.cshtml

 @model MvcApplication1.Models.ConcreteModel2 <h1>Concrete Model 2</h1> @Model.Content 

_ConcreteModel3.cshtml

 @model MvcApplication1.Models.ConcreteModel3 <h1>Concrete Model 3</h1> @Model.Content 

controller

 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); } <BaseModel> (); 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); } = "This is model 1."}); 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); } = "This is model 3."}); 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); } 
+5
source

You can have a composite view for TestView , and then display the partial representations for each of the smaller species. For this model to smaller representations should be made available from the model passed in the composite view.

Something like this: (TestView.cshtml)

 @model ParentViewModel @Html.RenderPartial("View1", Model.SubModel1) @Html.RenderPartial("View2", Model.SubModel2) @Html.RenderPartial("View3", Model.SubModel3) 

Then you have certain types, such as: (View1.cshtml)

 @model SubViewModel1 <!-- Whatever --> 
+3
source

I would create a custom model, the content of which was your IEnumerable<BaseModel> , which had its own built-in methods for a particular model of the type. So you can display partial submission and send them to a specific ConcreteModel , printed correctly. Maybe something like:

 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(); } } > x is T) .FirstOrDefault (); 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(); } } 

Then in your global view, you can display the parts with:

 RenderPartial("ConcreteViewName", Model.getModelByType<ConcreteModel1>()); 

Note. Treat the above as psuedo-code. Most likely, it will be fixed in order to work correctly.

0
source

All Articles