【发布时间】:2011-11-04 10:12:22
【问题描述】:
有什么方法可以将多个变量传递给视图,以便使用类型成员和方法?
我想让我的编码更安全? ViewData 不会传递强类型。
ASP.net 4 MVC 3 谢谢
【问题讨论】:
-
你在这里使用什么框架? ASP.NET MVC?还是什么?
标签: c# asp.net-mvc-3 strongly-typed-view
有什么方法可以将多个变量传递给视图,以便使用类型成员和方法?
我想让我的编码更安全? ViewData 不会传递强类型。
ASP.net 4 MVC 3 谢谢
【问题讨论】:
标签: c# asp.net-mvc-3 strongly-typed-view
通常这样做的方法是创建一个具有您需要的各种值的视图模型,即
public class SomeSpecificViewModel {
public int SomeValue {get;set;}
public string AnotherValue {get;set;}
public FancyType MoreComplexValue {get;set;}
}
现在将SomeSpecificViewModel 传递给视图,并告诉您的视图期待SomeSpecificViewModel。然后访问成员。
【讨论】:
@model SomeSpecificViewModel - 工作完成;当您访问Model.blah 时,您会注意到Model 是 SomeSpecificViewModel