【问题标题】:What is the best way to pass strongly typed variables to the view将强类型变量传递给视图的最佳方法是什么
【发布时间】:2011-11-04 10:12:22
【问题描述】:

有什么方法可以将多个变量传递给视图,以便使用类型成员和方法?

我想让我的编码更安全? ViewData 不会传递强类型。

ASP.net 4 MVC 3 谢谢

【问题讨论】:

  • 你在这里使用什么框架? ASP.NET MVC?还是什么?

标签: c# asp.net-mvc-3 strongly-typed-view


【解决方案1】:

通常这样做的方法是创建一个具有您需要的各种值的视图模型,即

public class SomeSpecificViewModel {
    public int SomeValue {get;set;}
    public string AnotherValue {get;set;}
    public FancyType MoreComplexValue {get;set;}
}

现在将SomeSpecificViewModel 传递给视图,并告诉您的视图期待SomeSpecificViewModel。然后访问成员。

【讨论】:

  • 我需要在视图中声明它吗?如果有,怎么做?
  • @yos 如果这是剃须刀,那么在顶部:@model SomeSpecificViewModel - 工作完成;当您访问Model.blah 时,您会注意到Model SomeSpecificViewModel
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 2011-04-04
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
相关资源
最近更新 更多