5

Is there any way of changing the binding prefix with a value which comes from the request parameters?

I have many nested search popups, and all of them shares the same ViewModel.

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)
{

}
1
  • Why do you change the binding prefix in your Action? Commented Jul 25, 2013 at 14:45

1 Answer 1

6

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)

Modify your action to the following

[HttpPost]
public ActionResult Search(SearchViewModel model)
{
    UpdateModel(model, model.BindingPrefix);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.