If I see your code you now add for each child a new model instance.
So if you have 10 children your modal html will be added 10 times to your document. This can cause problems because you have the same id for all of your modals.
A better solution would be to load the partial on your page once and then using jQuery for example open the modal and populate the fields using a ajax call and the Umbraco API.
Then in your foreach you could have a hyperlink where the href attribute contains the id of your IPublishedContent to pass in to your modal:
<a class="open-btn" href="#@page.Id">
Update
</a>
Assign click event to the hyperlinks to get the id do an ajax call, populate the modal fields and show the model:
$('.open-btn').click(function(e) {
e.preventDefault;
var docId = $(this).attr('href').split('#')[1];
// do ajax call and populate fields
// Open modal
$('.articleUpdateModal').modal('show');
});
Michael Thanks for the quick response, did you know any article or links that may help me about implementing ajax with the Umbraco API? I would love to read every details.. I will mark your answer as an solution.
Novice Problem need help
Hi,
I'm new to umbraco Its been 2 weeks from now since I started to learn umbraco this problem is simple.
Best Practices to pass child node in parameter using html action and load partial view which is a form with populated values.
here is my partial view macro
The controller
here is the modal
Any suggestion would be appreciated.
Hi Glen,
If I see your code you now add for each child a new model instance. So if you have 10 children your modal html will be added 10 times to your document. This can cause problems because you have the same id for all of your modals.
A better solution would be to load the partial on your page once and then using jQuery for example open the modal and populate the fields using a ajax call and the Umbraco API.
So load in the modal on your page:
Then in your foreach you could have a hyperlink where the href attribute contains the id of your
IPublishedContent
to pass in to your modal:Assign click event to the hyperlinks to get the id do an ajax call, populate the modal fields and show the model:
Hope this helps!
/Michaël
Hi,
Michael Thanks for the quick response, did you know any article or links that may help me about implementing ajax with the Umbraco API? I would love to read every details.. I will mark your answer as an solution.
Hi Glen,
forgot to mention the link: https://our.umbraco.org/documentation/reference/routing/webapi/
If you can't get it to work let us know!
/Michaël
And your ajax call could be something like this:
Where methodName and controllerName are the webapi elements to get the
IPublishedContent
based on the id from the hyperlink./Michaël
Hi Michael,
Many thanks so glad to hear that and yes I will if anything comes up I will let you know
Cheers,
No problem, glad I could help you out!
Enjoy your day.
/Michaël
is working on a reply...