2

Is it possible to dynamically add an Editor Template to my view, after a button is clicked, for example?

At the moment I'm doing this in my main view to bind a list of ObjectA objects to my model (inside a form):

@Html.EditorFor(x => x.ObjectA)

and in the Editor Template I'm binding the properties for ObjectA:

@Html.DisplayFor(x => x.ID)
@Html.CheckBoxFor(x => x.BoolA)

But if I don't want to always load this editor template, is there a way to bind it dynamically to the model, using JS?

It's because it makes an additional request to the Database which I can omit by allowing the user to choose whether or not to make the request.

Is this possible?

1 Answer 1

2

You can't do that with EditorFor on client because the razor is transformed in html on server side.

You can do this if you make use of PartialView, where you can include your EditorFor.

You make a ajax call with js for this PartialView, but in axaj call you have to specify the id to query that object inside action.

At the end you will append the result, html content, of the call in your page.

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

6 Comments

So you mean having a partial view in which I have EditorFor. Do you think doing this will bind to the model, however? Am I not going to have prefix problems?
I tried it and it doesn't bind to the model (obviously). I decided to add an extra boolean to my model which checks whether it should make the request or not so when the user clicks a button it refreshes the page with an updated model that decides whether or not to display more data based on the user's choice
@sparta223 You tried to make @Html.EditorFor(x => x.ObjectA) or @Html.EditorFor(x => x) ?
I made a partial view in which I only had @Html.EditorForModel() which was using ObjectA as the model
@sparta223 tis is the reason why didn't bind, you needed an empty model with property ObjectA and to use @Html.EditorFor(x => x.ObjectA)
|

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.