1

I am attempting to build an MVC form that uses ajax to post to a controller action which will return one of 2 partial views depending on the model state, and then perform a partial page update via jQuery. This part is all working fine.

The initial controller action that renders the page uses glass to populate form labels & service data to populate drop-down lists.

When posting the form data via ajax the controller action has no sitecore context so cannot re-render the glass model properties on subsequent requests. I don't really want to re-query sitecore & the call the external service on each attempted form post, so what would be the best approach in ensuring the sitecore & service data is available to the view model on each partial render?

My current thinking is to have separate renderings for experience editor (renders glass properties) & the published site (initial controller action populates view model with glass properties), and then when posting the form post the labels & drop-down options as hidden fields in order to be retrieved from the view model each time the partial page is reloaded.

With regular MVC you'd just use @Html.LabelFor but I naturally want that value to come from sitecore.

Read about the beginformroute & formhandler approach but this doesn't appear to offer anything & I've also read it cannot support returning partial views.

Looking for support on best practice here using sitecore v9.1

(typed on mobile device so cannot easily add code snippets. However don't believe this would have helped as it's just your regular MVC form cshtml, controller action returning a partial & js making an ajax call & updating a dom element via jQuery)

Thanks for any feedback & support on this 😊

1 Answer 1

0

Opted for storing the service data in session state between requests & re-querying the sitecore content via glass in the controller action that receives the form post. I then repopulate the viewmodel before re-rendering the partial view.

I note the returned markup in the partial contains the validation errors but they don't actually get rendered when updating the page via jQuery. Would appear I need to import the jQuery validate & unobtrusive libraries to resolve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.