How to define form field prefix in ASP.NET MVC
Asked Answered
H

1

17

I am attempting to render a composite ProductCatalog view in ASP.NET MVC. This requires that I render multiple Product views per page. Each product view is a separate form. I need the form fields to have a prefix based on the id, so that I don't have duplicate IDs in the rendered document. Is there a way to define a prefix to be applied to all the form fields that are generated by the Html Extensions, or do I need to build this out manually?

Hali answered 10/4, 2011 at 10:53 Comment(0)
P
30

Yes, you can define a prefix for the controls inside your view based on the executing action, consider the following code that should be place in your GET action method:

ViewData.TemplateInfo.HtmlFieldPrefix = "DESIRED_PREFIX";

this will add the required prefix to your View controls, but in order to deal with them back when you post your page, you will need to redefine the prefix in the signature of your POST action as the following:

public ActionResult Create([Bind(Prefix = "DESIRED_PREFIX")] YOUR_ENTITY model)

Let me know if it worked, thanks.

Pros answered 10/4, 2011 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.