How to Add an MVC partial view form to a page via RTE
Ok, I am starting an fresh thread on this topic as I feel it has gotten side tracked among the other posts. First may I start off by saying, it just ain't working!
I have a page in an Umbraco 6.1.5 site. In this page is a RTE field. In this RTE I wish to place a macro that points to an MVC partial view. As long as that view is not tied to a Model, all works well.
Now, I want to take things to the next level. I want this partial view to contain an HTML form that IS tied to a Model so that I can have strong types, validation and all the other goodness of MVC.
I followed all the many many posts online about how to do this. I created a simple Model such like:
namespace EthicsAdmin.Models
{
public class OpinionViewModel
{
public string opinion { get; set; }
public string title { get; set; }
}
}
I then, following others examples, I create a partial view (the one referenced in my Macro) that has nothing more than
@Html.Action("editOpinion", "OpinionsSurface")
I create a SurfaceController like this
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace EthicsAdmin.Controllers
{
public class OpinionsSurfaceController : SurfaceController
{
public ActionResult editOpinion()
{
return PartialView("OpinionEdit", new Models.OpinionViewModel());
}
}
}
I then create my partial view with nothing more than
@model EthicsAdmin.Models.OpinionViewModel
@using EthicsAdmin.Controllers.SurfaceControllers;
@using (Html.BeginUmbracoForm<OpinionsSurfaceController>(}))
{
<!-- Rendering the test form here -->
<input id="test0" value="blah" />
}
I build my solution, copy to the server and run the page. What I end up with is
A partial view macro can't have a controller because it's just a Razor file that can be embedded anywhere. What you might be looking for is route hijacking. You can put use controllers and custom models and pass those to your razor views, but that only works if your templates are razor views and not your macro.
You could place an html action in your macro, but I'm not sure if that best practice.
You could have a look at the Hybrid Framework. It has a ton of MVC examples with route hijacking and html.action.
Looking in the log proved helpful. At least it gives me the error details. I did a very simple test, based on a post in these forums. I went into the Developer section. I right click on Partial View Macro Files, Create. I get the basic file. I put this in and saved it.
I added the macro to a content page via RTE. Run the page, get generic error. Look in the log and I get this:
Error loading Partial View (file: ~/Views/MacroPartials/Admin/Ethics/TestPartial.cshtml). Exception: System.Web.HttpCompileException (0x80004005): d:\WebSites\BarCentral\Views\MacroPartials\Admin\Ethics\TestPartial.cshtml(2): error CS0234: The type or namespace name 'Cms' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)
Is there a best practices guide on how to build these things? Before MVC I would create a UserControl for say a contact form or update form that would run inside the Umbraco site. But with MVC the game changes. Even if I used an iFRame I would still need to be able to create URLs that don't conflict with Umbraco.
I found examples out there that seem to place a basic Razor view in the macro and that view then calls the actual MVC partial view with the form or the form action. Just does not seem to be any indication of what's the recommended way vs something someone hacked that just seems to work.
Bump. Anyone have an update? So far it looks like I can perform the VC portion of MVC, just not the M. I can create a razor view, I can use jQuery and AJAX to perform data reads and updates via a Controller. I just cannot hookup the Model so I can validate and strongly type my fields on my form.
It's not directly answering the model passing question, but for Macros with forms, I have been creating a named version of my model, using that for the form, and then setting Prefix on the Bind of the controller.
so , the form might have
var opinion = new OpinionViewModel();
using (Html.BeginUmbracoForm<....>())
{
@Html.TextBoxFor(m => opinion.title);
}
Then in the controller
public ActionResult editOpinion([Bind(Prefix="opinion")]OpinionViewModel model])
it's not as neat as passingt the model straight over, which i don't think you can do with the Macro class but you can do with Partial views with UmbracoViewPage ?
I have been trying different things and this is the latest error I get:
Error loading Partial View (file: ~/Views/MacroPartials/Admin/Ethics/OpinionEdit.cshtml). Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'EthicsAdmin.Models.OpinionViewModel'.
How to Add an MVC partial view form to a page via RTE
Ok, I am starting an fresh thread on this topic as I feel it has gotten side tracked among the other posts. First may I start off by saying, it just ain't working!
I have a page in an Umbraco 6.1.5 site. In this page is a RTE field. In this RTE I wish to place a macro that points to an MVC partial view. As long as that view is not tied to a Model, all works well.
Now, I want to take things to the next level. I want this partial view to contain an HTML form that IS tied to a Model so that I can have strong types, validation and all the other goodness of MVC.
I followed all the many many posts online about how to do this. I created a simple Model such like:
I then, following others examples, I create a partial view (the one referenced in my Macro) that has nothing more than
I create a SurfaceController like this
I then create my partial view with nothing more than
I build my solution, copy to the server and run the page. What I end up with is
Like I said, IT JUST DON'T WORK! WFT? There is absolutely no way to debug this, no way to access the trace.
Hi Connie
Have you had a look in the /App_Data/Logs file? There should be a more detailed description of the error in there.
/Jan
I saw incorrect } in your post:
Is it the typo in your post or your code?
Hello,
A partial view macro can't have a controller because it's just a Razor file that can be embedded anywhere. What you might be looking for is route hijacking. You can put use controllers and custom models and pass those to your razor views, but that only works if your templates are razor views and not your macro.
You could place an html action in your macro, but I'm not sure if that best practice.
You could have a look at the Hybrid Framework. It has a ton of MVC examples with route hijacking and html.action.
Jeroen
Looking in the log proved helpful. At least it gives me the error details. I did a very simple test, based on a post in these forums. I went into the Developer section. I right click on Partial View Macro Files, Create. I get the basic file. I put this in and saved it.
I added the macro to a content page via RTE. Run the page, get generic error. Look in the log and I get this:
Say what? CMS does not exist in Umbraco?
Jeroen,
Is there a best practices guide on how to build these things? Before MVC I would create a UserControl for say a contact form or update form that would run inside the Umbraco site. But with MVC the game changes. Even if I used an iFRame I would still need to be able to create URLs that don't conflict with Umbraco.
I found examples out there that seem to place a basic Razor view in the macro and that view then calls the actual MVC partial view with the form or the form action. Just does not seem to be any indication of what's the recommended way vs something someone hacked that just seems to work.
Bump. Anyone have an update? So far it looks like I can perform the VC portion of MVC, just not the M. I can create a razor view, I can use jQuery and AJAX to perform data reads and updates via a Controller. I just cannot hookup the Model so I can validate and strongly type my fields on my form.
Hi
It's not directly answering the model passing question, but for Macros with forms, I have been creating a named version of my model, using that for the form, and then setting Prefix on the Bind of the controller.
so , the form might have
Then in the controller
it's not as neat as passingt the model straight over, which i don't think you can do with the Macro class but you can do with Partial views with UmbracoViewPage ?
@inherits Umbraco.Web.Mvc.UmbracoViewPage<OpinionModel>
I have been trying different things and this is the latest error I get:
Ok, it seems like a longer road than it should but I got my form to display.
My macro points to a partial view that does nothing more than load another partial view, but also passes the model.
Now I have a form. I should be able to use jQuery and AJAX to populate this form and save changes to the form.
is working on a reply...