0

I want to create a view that can be used twice on the same page, each time with a unique id.

At the moment I'm using two approaches to do this kind of thing

  1. Editor Templates
  2. stevensanderson's approach for having controls created on the fly

Editor Templates are the easiest as they take of the name mangling (e.g. parent1_parent2_childId) but Editor Templates can't be used across Areas and I really like Areas

Is there a 3rd option?

thanks

1 Answer 1

1

You can pass a model to a child view in the RenderPartial extension method. Use this mode to pass a desired id, or something from which it can be obtained.

Sign up to request clarification or add additional context in comments.

5 Comments

I haven't tried yet but I'm guessing there are caveats here. if you put two controls in the same form they will break model binding, either because we change the name (can't bind) or we don't change the name (binds randomly). However if not in the same form then it should work?
You can set the model to be dynamic, this way, you can pass anything.
I wasn't aware of the dynamic keyword in Views so I've been looking into it. It's surprisingly difficult to find an example of array binding, e.g. inside '@foreach (dynamic item in Model)' what would you use to bind it? I've tried @Html.TextBox("item.A") and variants. Probably a whole new question though. Thanks for the help so far
Store your collection in the dynamic instance, in some property (.Items). Then, in your view, do this: foreach (var item in Model.Items.OfType<MyClass>()) { ... }
I couldn't get it working but I've reached the conclusion that dynamic isn't the way to go anyway. I'll stick with Editor Templates. Thanks

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.