Cat*_*lin 4 asp.net asp.net-mvc model-binding
有没有办法用一个来自请求参数的值更改绑定前缀?
我有许多嵌套的搜索弹出窗口,并且它们都共享相同的ViewModel.
我可以在请求搜索过滤器时为所有字段添加绑定前缀,但我不知道如何使[Bind(Prefix ="")]使用来自请求参数的值.
// 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)
{
}
Run Code Online (Sandbox Code Playgroud)
我不知道你为什么要这样做,但这应该有效.
在视图的表单中,有一个隐藏的值
@Html.Hidden("BindingPrefix", Model.BindingPrefix)
Run Code Online (Sandbox Code Playgroud)
将您的操作修改为以下内容
[HttpPost]
public ActionResult Search(SearchViewModel model)
{
UpdateModel(model, model.BindingPrefix);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
976 次 |
| 最近记录: |