Asp.Net MVC动态模型绑定前缀 [英] Asp.Net MVC Dynamic Model Binding Prefix
本文介绍了Asp.Net MVC动态模型绑定前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用来自请求参数的值来更改绑定前缀?
Is there any way of changing the binding prefix with a value which comes from the request parameters?
我有很多嵌套的搜索弹出窗口,并且它们都共享相同的ViewModel.
I have many nested search popups, and all of them shares the same ViewModel.
我可以在请求搜索过滤器时向所有字段添加绑定前缀,但是我不知道如何使[Bind(Prefix =")]与来自请求参数的值一起使用.
I can add a binding prefix to all the fields when requesting for the Search filters, but i don't know how can i make the [Bind(Prefix = "")] to work with values coming from the request parameters.
// get the search filters with the bindingPrefix we need
public ActionResult Search(string bindingPrefix)
{
ViewData.TemplateInfo.HtmlFieldPrefix = bindingPrefix;
SearchViewModel model = new SearchViewModel
{
BindingPrefix = bindingPrefix
};
return PartialView("_SearchFilters", model);
}
// post the search filters values
[HttpPost]
public ActionResult Search([Bind(Prefix = model.BindingPrefix)]SearchViewModel model)
{
}
推荐答案
我不知道您为什么要这样做,但这应该可行.
I don't know why you would want to do this, but this should work.
在视图上的表单中,具有隐藏值
In your form on the view, have a hidden value
@Html.Hidden("BindingPrefix", Model.BindingPrefix)
将操作修改为以下内容
[HttpPost]
public ActionResult Search(SearchViewModel model)
{
UpdateModel(model, model.BindingPrefix);
}
这篇关于Asp.Net MVC动态模型绑定前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文