【问题标题】:Asp.Net MVC Dynamic Model Binding PrefixAsp.Net MVC 动态模型绑定前缀
【发布时间】:2013-07-25 23:45:52
【问题描述】:

有没有办法用来自请求参数的值更改绑定前缀?

我有许多嵌套的搜索弹出窗口,它们都共享同一个 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)
{

}

【问题讨论】:

  • 为什么要更改 Action 中的绑定前缀?

标签: asp.net asp.net-mvc model-binding


【解决方案1】:

我不知道您为什么要这样做,但这应该可以。

在你的表单上的视图中,有一个隐藏值

@Html.Hidden("BindingPrefix", Model.BindingPrefix)

将您的操作修改为以下内容

[HttpPost]
public ActionResult Search(SearchViewModel model)
{
    UpdateModel(model, model.BindingPrefix);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多