How do I integrate an MVC form into the Umbraco CMS?
I've created an MVC form using a PartialView and a SurfaceController using the examples on this website, but I'm having problems figuring out how to integrate that form into the CMS. Basically, I want my PartialView to replace the page text that is normally defined by my document type.
I initially thought that a PartialView macro would work, because I could just add it in to the rich text editor, but I have run into problems with it. Specifically, the only way I can get the macro to work is to put in an @Html.Action(...) pointing to my controller and view, but that causes an unnecessary call to my controller when I do a return CurrentUmbracoPage() from the controller.
What is the official (or best unofficial) way to integrate a PartialView form into the regular CMS content?
What I am trying to do is convert a site using Umbraco v4 and MvcBridge to Umbraco v6. I actually tried to delete this question, as more research proved to me that I cannot easily move my code from MvcBridge to SurfaceControllers, as they don't work the same way. Primarily, MvcBridge allows me to return any PartialView I want out of a POST, which my code does fairly regularly. Changing all of that code to work with "return CurrentUmbracoPage()" and putting all of the logic regarding what to display in the View is more work than I am willing to undertake right now.
You can use regular controllers with Umbraco aswell as return results other than CurrentUmbracoPage() from your actiona. Surface controllers basically allow you to maintain context within your postback so Umbraco knows what "page" you were on when you submitted the form. If you aren't bothered about context, you can just use a regular controller and return a regular view, or if you just want context within your controller, but want to return a something other than currentumbracopage, you can.
SurfaceControllers are the simplest way to postback whilst maintainin context, but they certainly aren't the only way. Just another tool in the toolbox.
I'm struggling with the perception that every tool in the toolbox has some issue that makes it not work the same way Umbraco v4 + MvcBridge worked. In my existing site, my MVC site simply concerns itself with all the fancy form logic inside of PartialViews. It doesn't know or care about Umbraco at all. The MvcBridge macro is dropped into a RichTextEditor on my document and pointed at the correct MVC controller and everything works.
I cannot seem to get the same functionality with SurfaceControllers and PartialView macros, or with hijacking routes, but maybe I'm misunderstanding how things work.
Currently I've changed over to creating an MVC area that contains my MVC code. The views in the area call SurfaceControllers defined in Umbraco in order to build the page layout so that it looks the same as if it were inside Umbraco. It's a bit messy, but at least my MVC code is separated out and does not need to be changed.
I must admit, I haven't tried dropping a PartialViewMacro in an RTE, I tend to create specific doc types and have the doc types template include the form after the main content via @Html.Partial("FormPartial"). From there I'll just do the usual SurfaceController begin form code and have it postback to my surface controller, and then from there do some action and redirect or return to current page. I've not returned a partial view before, but I have returned JSON with no problem, so would have thought that was somewhat similar.
I must also admit, I haven't done much with the MvcBridge either, so couldn't really say how well the concept fits with SurfaceControllers and PartialViewMacros.
Can you explain what the end result should be? Should the form just postback and return to the current page showing a second state for your form partial view? or should it be doing something else?
That's exactly what I want. Essentially, I have a bunch of business functionality that must exist within the same website as the main CMS. To keep things simple, I just drop the MvcBridge macro into the main page text RTE. The form (which is a PartialView contained within an existing Umbraco page) POSTs to the controller, which can then do multiple things:
Return the PartialView due to validation failing.
Redirect to a different page completely.
Return a completely different PartialView (generally a specific message for when certain validations fail, such as not having the security rights to perform the action).
#3 does not work, because returning a PartialView from the POST causes the main Umbraco rendering to not occur, so all you get is the PartialView and nothing from the rest of your page.
I'm not sure if this is ideal in your scenario and expect you've already thought of it... but if you can't get #3 to work you could fall back to a single PartialView but with some logic to display different things depending on what validation errors have occurred. E.g.
if (ViewData.ModelState["UnauthorisedAccess"] != null)
{
// Display custom message
}
else
{
// Display form
}
How do I integrate an MVC form into the Umbraco CMS?
I've created an MVC form using a PartialView and a SurfaceController using the examples on this website, but I'm having problems figuring out how to integrate that form into the CMS. Basically, I want my PartialView to replace the page text that is normally defined by my document type.
I initially thought that a PartialView macro would work, because I could just add it in to the rich text editor, but I have run into problems with it. Specifically, the only way I can get the macro to work is to put in an @Html.Action(...) pointing to my controller and view, but that causes an unnecessary call to my controller when I do a return CurrentUmbracoPage() from the controller.
What is the official (or best unofficial) way to integrate a PartialView form into the regular CMS content?
Hi, What is the end result you want to achieve here?
Surely When your page loads, it loads a view say Forms?
Then you may have some partial view called RegistrationForm, ContactForm ect ect
If you use AJAX you can call to the controller and action in your partial view and your action will return a partial view result.
Thus the result of action will be returned to the page.
Does this make sense? Sorry if i have not explained it propertly. If you need something else, just ask :)
What I am trying to do is convert a site using Umbraco v4 and MvcBridge to Umbraco v6. I actually tried to delete this question, as more research proved to me that I cannot easily move my code from MvcBridge to SurfaceControllers, as they don't work the same way. Primarily, MvcBridge allows me to return any PartialView I want out of a POST, which my code does fairly regularly. Changing all of that code to work with "return CurrentUmbracoPage()" and putting all of the logic regarding what to display in the View is more work than I am willing to undertake right now.
Hi Jason,
You can use regular controllers with Umbraco aswell as return results other than CurrentUmbracoPage() from your actiona. Surface controllers basically allow you to maintain context within your postback so Umbraco knows what "page" you were on when you submitted the form. If you aren't bothered about context, you can just use a regular controller and return a regular view, or if you just want context within your controller, but want to return a something other than currentumbracopage, you can.
SurfaceControllers are the simplest way to postback whilst maintainin context, but they certainly aren't the only way. Just another tool in the toolbox.
Hope that helps.
Matt
Matt,
I'm struggling with the perception that every tool in the toolbox has some issue that makes it not work the same way Umbraco v4 + MvcBridge worked. In my existing site, my MVC site simply concerns itself with all the fancy form logic inside of PartialViews. It doesn't know or care about Umbraco at all. The MvcBridge macro is dropped into a RichTextEditor on my document and pointed at the correct MVC controller and everything works.
I cannot seem to get the same functionality with SurfaceControllers and PartialView macros, or with hijacking routes, but maybe I'm misunderstanding how things work.
Currently I've changed over to creating an MVC area that contains my MVC code. The views in the area call SurfaceControllers defined in Umbraco in order to build the page layout so that it looks the same as if it were inside Umbraco. It's a bit messy, but at least my MVC code is separated out and does not need to be changed.
Hi Jason,
I must admit, I haven't tried dropping a PartialViewMacro in an RTE, I tend to create specific doc types and have the doc types template include the form after the main content via @Html.Partial("FormPartial"). From there I'll just do the usual SurfaceController begin form code and have it postback to my surface controller, and then from there do some action and redirect or return to current page. I've not returned a partial view before, but I have returned JSON with no problem, so would have thought that was somewhat similar.
I must also admit, I haven't done much with the MvcBridge either, so couldn't really say how well the concept fits with SurfaceControllers and PartialViewMacros.
Can you explain what the end result should be? Should the form just postback and return to the current page showing a second state for your form partial view? or should it be doing something else?
Many thanks
Matt
Matt,
That's exactly what I want. Essentially, I have a bunch of business functionality that must exist within the same website as the main CMS. To keep things simple, I just drop the MvcBridge macro into the main page text RTE. The form (which is a PartialView contained within an existing Umbraco page) POSTs to the controller, which can then do multiple things:
I'm not sure if this is ideal in your scenario and expect you've already thought of it... but if you can't get #3 to work you could fall back to a single PartialView but with some logic to display different things depending on what validation errors have occurred. E.g.
Hi
Could you switch to use an Ajax form?
then you can target the area of the page you want to update with your error message
is working on a reply...