asp.net-mvc-3 – 构造函数注入用作Action方法参数的View Model实例
|
创建视图模型时,您可以将选项(例如,在下拉列表中使用)填充到视图模型的setter属性中.
我在这个问题中特别要求的一个潜在解决方案是如何使MVC框架使用构造函数注入实例化视图模型,这将为视图模型构造函数提供某种数据访问对象的实现(例如,存储库) )当视图请求选项时,它们可用于检索选项(例如,在辅助方法“DropDownListFor”中)? 我认为该解决方案可能与IModelBinderProvider或IModelBinder的实现有关,但是在网络上的示例代码片段中对这些事情进行了实验后,我仍在寻找一个完全可行的示例,可下载的可执行代码没有任何遗漏将所有事物放在一起的方法. 如果您正在寻找有关如何填充选择列表的替代讨论,例如使用“Dependecy Lookup”而不是“Dependecy Injection”,您可能需要查看以下讨论: 几天前,我在该帖子中写了关于“Dependecy Injection”的以下后续问题,我正在寻找这个帖子: 但是,我没有希望有人会找到那个标题较少的旧帖子,而是用一个更具体的主题来创建这个新问题,我正在寻找什么. 解决方法我假设你想让你的ViewModel通过他们的构造函数自动注入一些东西 – 例如某种配置对象,View将用它来确定要显示的内容.我还假设当MVC尝试从Controller Action的参数自动创建和绑定模型实例时,这种方法导致“为此对象定义的无参数构造函数”错误.然后我们假设我们将使用DI框架在运行时自动将SiteConfig对象注入到我们的控制器中.这意味着我们必须解决的唯一问题是如何在Controller自动绑定时将Controller中的注入对象引入其Actions的ViewModel. 因此,让我们为其他人继承基础模型. BaseViewModel public class BaseViewModel
{
public ISiteConfig SiteConfig { get; set; }
public BaseViewModel(ISiteConfig siteConfig)
{
this.SiteConfig = siteConfig;
}
}
现在让我们创建一个继承它的模型. IndexViewModel public class IndexViewModel : BaseViewModel
{
public string SomeIndexProperty { get; set; }
public IndexViewModel (ISiteConfig siteConfig) : base(siteConfig) {}
}
现在让我们定义一个控制器将继承的基本控制器. BaseController public abstract class BaseController : Controller
{
protected BaseController(ISiteConfig siteConfig)
{
_siteConfig = siteConfig;
}
private readonly ISiteConfig _siteConfig;
public ISiteConfig SiteConfig
{
get
{
return _siteConfig;
}
}
}
现在我们定义实际的控制器. HomeController的 public HomeController: BaseController
{
public HomeController(ISiteConfig siteConfig): base(siteConfig) {}
}
假设我们将Ninject用于DI,Ninject将被配置为自动创建Controller并在运行时将具体的ISiteConfig对象传递给其构造函数. 现在我们将Action添加到Controller. 指数行动 public ActionResult Index(IndexViewModel model)
{
return View(model);
}
因此,如果您尝试调用Index Action,MVC将在没有做任何其他事情的情况下爆炸,并且会出现“无参数构造函数”错误,因为MVC无法找到不带参数的ViewModel构造函数. 所以,答案.我们需要覆盖默认的ModelBinder. BaseViewModelBinder public class BaseViewModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)
{
if (modelType == typeof(BaseViewModel) || modelType.IsSubclassOf(typeof(BaseViewModel)))
{
var baseControl = controllerContext.Controller as BaseController;
if (baseControl == null)
{
throw new Exception("The Controller must derive from BaseController");
}
var instance = Activator.CreateInstance(modelType,baseControl.SiteConfig);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance,modelType);
return instance;
}
else
{
return base.CreateModel(controllerContext,bindingContext,modelType);
}
}
}
我们需要将它设置为global.asax.cs中的默认模型绑定器: protected void Application_Start()
{
...
ModelBinders.Binders.DefaultBinder = new BaseViewModelBinder();
}
就这样.如您所见,当您立即查看索引操作时,MVC将使用我们的自定义模型绑定器.它将意识到IndexViewModel派生自BaseViewModel,因此将尝试使用它可以在Action的Controller中找到的ISiteConfig来启动IndexViewModel实例(因为Controller派生自BaseController). (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – asp.net mvc中HttpUnauthorizedResult上的默
- asp.net – 如何在.net WebApi2应用程序中使用OAuth2令牌请
- ASP.NET Web应用程序(.NET Framework)与ASP.NET核心Web应用
- asp.net – 为什么Visual Studio会拒绝访问,尝试使用NuGet软
- asp.net – gzip压缩在Windows Azure网站
- 远程服务器返回错误:(401)未经授权.在ASP.NET中使用CSOM
- 在经典ASP中对集合进行排序
- asp.net-mvc-3 – mvc3 httpshttp
- asp.net-mvc-4 – ASP.NET MVC 4单独项目中的区域不工作(查
- asp.net – Visual Studio IIS Express不工作
- asp.net-mvc – 为什么ListBoxFor不选择项目,但L
- asp.net-mvc – Umbraco 7自定义cookie
- asp.net – 如何将IIS Developer Express切换到“
- asp.net-mvc – Bower,Grunt和Yeoman如何适应Vis
- asp.net – 对于布尔查询字符串参数使用“true”
- asp.net – Web Api参数始终为null
- asp.net – 连接到源文件XYZ的撤消管理器时出错
- 过期输出缓存ASP.Net MVC
- asp.net-mvc – Actionresult vs JSONresult
- asp.net-mvc – 使用ASP.NET MVC中绝对路径访问视
