且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎样才能创建MVC通用视图模型?

更新时间:2022-10-15 16:54:51

我相信,控制器和视图之间传递数据的predominate的方法是创建一个类,再presents要传递的数据你的观点,并通过该模型变量视图的方法。

/Models/Home/IndexViewModel.cs

 命名空间MyProject.Models.Home
{
  公共类IndexViewModel
  {
    公共字符串消息{搞定;组; }
  }
}

控制器/ HomeController.cs

 公共类HomeController的
{
  公众的ActionResult指数()
  {
    IndexViewModel模式=新IndexViewModel();
    model.Message =的Hello World!;
    视图(模型);
  }
}

/Views/Home/Index.cshtml (适用于刀片MVC3)

  @Model MyProject.Models.Home.IndexViewModel //强类型的视图< H1> @ model.Message< / H1>

让我们以这个简单的例子,并逐步建立您的具体要求。最简单的方法是让每个视图只需使用 MyClass的模式。然而,这变得非常不灵活,所以这里是我会做什么,以保持设计的灵活性。我要去假设有你想传递给很多意见(部分或全部)。一些数据

创建类,重presents你想传递给多个视图中的数据:

/Models/SharedData.cs

 命名空间MyProject.Models
{
  公共类SharedData
  {
    公众的DateTime生日{搞定;组; }
  }
}

创建一个接口,用于模型要求SharedData类。

/Models/ISharedDataViewModel.cs

 命名空间MyProject.Models
{
  公共接口ISharedDataViewModel
  {
    公共SharedData数据{搞定;组; }
  }
}

更新主页IndexViewModel使用接口和shareddata ​​ P>

/Models/Home/IndexViewModel.cshtml

 命名空间MyProject.Models.Home
{
  公共类IndexViewModel:ISharedDataViweModel
  {
    公共字符串消息{搞定;组; }
    公共ShardedData数据{搞定;组; }
  }
}

创建一个知道如何显示共享数据的局部视图

/Views/Shared/SharedDataView.cs (适用于刀片MVC3)

  @Model MyProject.Models.ISharedDataViewModel //强类型的局部视图@if(型号= NULL&放大器;!&安培;!model.Data = NULL)
{
&所述; H3> @ model.Data.Birthday.ToString()&下; / H3>
}

更新首页索引页调用局部视图

/Views/Home/Index.cshtml (适用于刀片MVC3)

  @Model MyProject.Models.Home.IndexViewModel //强类型的视图< H1> @ model.Message< / H1>@ Html.Partial(SharedDataView模型)

现在任何页面可以拨打电话进行局部视图如果页面模型实现 ISharedDataViewModel

I would like to use a viewmodel in MVC instead of using viewbag. Is there a way that I could create some generic viewmodel that's shared between all my controllers and then use this in my views? What kind of code would I need for this? I was thinking to maybe create something in a base controller. Is that possible?

I believe the predominate method for passing data between controllers and views is to create a class that represents the data you want to pass to your view and pass that model variable to the view method.

/Models/Home/IndexViewModel.cs

namespace MyProject.Models.Home
{
  public class IndexViewModel
  {
    public string Message { get; set; }
  }
}

Controllers/HomeController.cs

public class HomeController
{
  public ActionResult Index()
  {
    IndexViewModel model = new IndexViewModel();
    model.Message = "Hello World!";
    View(model);
  }
}

/Views/Home/Index.cshtml (in Razor MVC3)

@Model MyProject.Models.Home.IndexViewModel //strongly-typed view

<h1>@model.Message</h1>

Let's take this simple example, and build towards your specific request. The easy way would be to allow every view simply use the MyClass model. However, this becomes very inflexible, so here is what I would do to keep the design flexible. I'm going to assume there is some data you want to pass to many views (some or all).

Create class that represents the data you want to pass to multiple views:

/Models/SharedData.cs

namespace MyProject.Models
{
  public class SharedData
  {
    public DateTime Birthday { get; set; }
  }
}

Create an interface for models to require a SharedData class.

/Models/ISharedDataViewModel.cs

namespace MyProject.Models
{
  public interface ISharedDataViewModel
  {
    public SharedData Data { get; set; }
  }
}

Update the Home IndexViewModel to use the interface and shareddata

/Models/Home/IndexViewModel.cshtml

namespace MyProject.Models.Home
{
  public class IndexViewModel: ISharedDataViweModel
  {
    public string Message { get; set; }
    public ShardedData Data { get; set; }
  }
}

Create a Partial view that knows how to display shared data

/Views/Shared/SharedDataView.cs (in Razor MVC3)

@Model MyProject.Models.ISharedDataViewModel //strongly-typed partial view

@if (model != null && model.Data != null)
{
<h3>@model.Data.Birthday.ToString()</h3>
}

Update the Home Index page to call the Partial view

/Views/Home/Index.cshtml (in Razor MVC3)

@Model MyProject.Models.Home.IndexViewModel //strongly-typed view

<h1>@model.Message</h1>

@Html.Partial("SharedDataView", model) 

Now any page can call for the partial view if the pages model implements ISharedDataViewModel.