9
当我熟悉Asp.Net MVC时,我使用的是MVC 2,我注意到在Kigg项目中使用BaseViewData类,我不确定如何实行。如何在Asp.net MVC中使用Base ViewModel 2
我希望每个ViewModel都有一定的可用值。想到使用iterface,但我想知道最佳做法是什么,Kigg如何做到这一点?
Kigg
public abstract class BaseViewData
{
public string SiteTitle { get; set; }
// ...other properties
}
public class UserListViewData : BaseViewData
{
public string Title { get; set; }
// .. other stuff
}
在我的WebForms应用程序,我使用继承自System.Web.UI.Page一个BasePage的。
所以,在我的MVC项目,我有这样的:
public abstract class BaseViewModel
{
public int SiteId { get; set; }
}
public class UserViewModel : BaseViewModel
{
// Some arbitrary ViewModel
}
public abstract class BaseController : Controller
{
private IUserRepository _userRepository;
protected BaseController()
: this(
new UserRepository())
{
}
}
引用的Kigg方法,我该如何确保我的每一个视图模型的从BaseViewModel继承有SITEID财产?
什么是我应该使用的最佳做法,样本或模式?
你必须投过滤器上下文中使用这样的:在我的代码'((的ViewResult)filterContext.Result).ViewData.Model' – ajbeaven 2012-01-13 10:35:21
看,我竟然将结果保存在一个局部变量使用'as'投射到ViewResult,然后检查是否为空。 – tvanfosson 2012-01-13 12:39:41