sparta223
sparta223

Reputation: 293

ASP.NET MVC dynamically bind editor template

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?

Upvotes: 2

Views: 1338

Answers (1)

adricadar
adricadar

Reputation: 10219

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.

Upvotes: 2

Related Questions