Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Connie DeCinko 931 posts 1160 karma points
    Feb 25, 2015 @ 01:10
    Connie DeCinko
    0

    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

    Error loading Partial View script (file: ~/Views/MacroPartials/Admin/Ethics/Opinions.cshtml)
    

    Like I said, IT JUST DON'T WORK! WFT? There is absolutely no way to debug this, no way to access the trace.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 25, 2015 @ 05:23
    Jan Skovgaard
    0

    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

  • Owen 123 posts 246 karma points
    Feb 25, 2015 @ 08:45
    Owen
    0

    I saw incorrect } in your post:

    @using (Html.BeginUmbracoForm<OpinionsSurfaceController>(}))
    

    Is it the typo in your post or your code?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 25, 2015 @ 12:11
    Jeroen Breuer
    0

    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

  • Connie DeCinko 931 posts 1160 karma points
    Feb 25, 2015 @ 16:16
    Connie DeCinko
    0

    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.

    @inherits PartialViewMacroPage
    @using Umbraco.Cms.Web
    @using Umbraco.Cms.Web.Macros
    @using Umbraco.Framework
    
    <h1>Hello @DynamicModel.Name</h1>
    

    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?)
    

    Say what? CMS does not exist in Umbraco?

  • Connie DeCinko 931 posts 1160 karma points
    Feb 25, 2015 @ 16:32
    Connie DeCinko
    0

    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.

  • Connie DeCinko 931 posts 1160 karma points
    Mar 02, 2015 @ 17:59
    Connie DeCinko
    0

    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.

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Mar 02, 2015 @ 18:16
    Kevin Jump
    0

    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

    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 ?

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<OpinionModel>
  • Connie DeCinko 931 posts 1160 karma points
    Mar 02, 2015 @ 20:35
    Connie DeCinko
    0

    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'.
    
  • Connie DeCinko 931 posts 1160 karma points
    Mar 03, 2015 @ 18:59
    Connie DeCinko
    0

    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.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using EthicsAdmin.Controllers.SurfaceControllers
    @using EthicsAdmin.Models
    
    @Html.Partial("~/Views/MacroPartials/Admin/Ethics/_OpinionEdit.cshtml", new OpinionViewModel())
    

    Now I have a form. I should be able to use jQuery and AJAX to populate this form and save changes to the form.

Please Sign in or register to post replies

Write your reply to:

Draft