Copied to clipboard

Flag this post as spam?

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


  • Utte 7 posts 27 karma points
    Apr 03, 2014 @ 14:47
    Utte
    0

    Issue with custom controller

    Hi! I'm having troubles creating a custom controller to pass a model to my view. I have followed this http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers but i cant get it to work. At the moment my viewdata and viewbag doesnt pass any data and my "@Model.name" causes an error witch is "'Umbraco.Web.Models.RenderModel' does not contain a definition for 'name'", even though I have the name-attribute in my model. I don't know what to do to make this work, can someone please help me?

    This is my model:

    namespace UmbracoAzureFredrik.Models
    {
         public class Artist
         {
              public string name { get; set; }
         }
    
     }
    

    This is my controller:

    namespace UmbracoAzureFredrik.Controllers
    {
        public class VisaController : Umbraco.Web.Mvc.RenderMvcController
        {
            public override ActionResult Index(RenderModel model)
            {
                Artist amodel = new Artist();
                amodel.name = "Felix";
    
                ViewData["test"] = "test viewdata";
                ViewBag.test = "test viewbag";
                return CurrentTemplate(amodel);
            }
        }
    }
    

    This is my view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>
    @{
        Layout = "umbLayout.cshtml";
    }
    
    @ViewBag.test
    @ViewData["test"]
    @Model.name
    
  • Steve Jones 36 posts 84 karma points
    May 08, 2014 @ 09:38
    Steve Jones
    0

    I have a similar issue with custom model and controllers

    My error seems to return RenderModel to the view and ignore the custom model I passed in

  • Karl Svensson 9 posts 29 karma points
    Oct 22, 2014 @ 12:39
    Karl Svensson
    0

    Hi I am interested in the solution of this issue? Did you get it? Karl

  • Utte 7 posts 27 karma points
    Oct 22, 2014 @ 22:34
    Utte
    0

    We couldn't make it work so we used javascript instead.

  • Steve Jones 36 posts 84 karma points
    Oct 22, 2014 @ 22:44
    Steve Jones
    0

    I managed to create a custom model and controller

    In the view I used the approach of using a single doc type per controller and multiple templates, with one for each controller action.

    I really wanted to use a partial view or a macro but currently that's not supported

     

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Oct 23, 2014 @ 08:32
    Dave Woestenborghs
    0

    You need to change the first line of your view :

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<UmbracoAzureFredrik.Models.Artist>

    But i think this will break your base layout. 

    Can I ask why you are using custom view models ?

    Maybe you should have a look at the ZBU Models builder. This generates strongly typede models for your document types :

    https://github.com/zpqrtbnk/Zbu.ModelsBuilder

    And in this video Stéphane explains it in this video : http://uhangout.co.uk/video/NTzb7qQlNzM

    Dave

     

     

  • Karl Svensson 9 posts 29 karma points
    Oct 23, 2014 @ 12:23
    Karl Svensson
    0

    I want to connect with EF Models (in existing database) and render data to Umbraco Views.

    Dave, as you explain -sending the models with @inherits UmbracoViewPage<>- fails because the Models must inherit from RenderModel.

    Now I am trying to create a ViewModel class that map this. I will explain it later.

  • Steve Jones 36 posts 84 karma points
    Oct 23, 2014 @ 23:20
    Steve Jones
    0

    I have been through similar issues and managed to get a working package out there using custom model and controllers with a strongly typed view.

    I was also asked why I am using the Umbraco pipeline and using a custom model but when building an integration package I kind of need to.

    If you want I can did out the source to show you.

  • Karl Svensson 9 posts 29 karma points
    Oct 24, 2014 @ 09:20
    Karl Svensson
    0

    I would appreciate that Steve!

    My web application must interact with data managed in Touchscreen desktop computers, where web environment is not possible.

    I have been trying diferent packages, but all is still a bit too complex for me. It is being difficult to find precise updated information to do simple things...

  • Steve Jones 36 posts 84 karma points
    Oct 25, 2014 @ 01:00
    Steve Jones
    0

    Consistent documentation has been an issue I agree. I have offered my services to the Umbraco HQ to help build up documentation but I am still waiting for a reply.

    Anyhow, my view and controller snippets are below. My model is a class library which I access using a repository pattern.

    How you access your model I guess is up to you.

    Here's my view, which imherits from Umbraco but I cast it to my model to give me something like a strongly typed view. Because I'm using a custom model and controller and I don't inherit from the RenderModel in my model I have to do this.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>

    @using Umbraco.Web.Models

    @using LuminousAcuity.Model

    @{

        Layout = null;

        List<Organisation> organisations = (List<Organisation>)Model;

        

        RenderModel m = new RenderModel(Umbraco.Content(UmbracoContext.Current.PageId));

        IPublishedContent p = m.Content.Children.Where(x => x.DocumentTypeAlias == "Organisation").FirstOrDefault();

                    //I have to use a querystring format for the URL because Umbraco doesnt do custom route if you have custom model amd controller

    }

    <h2>@m.Content.GetPropertyValue("title")</h2>

    <section>

        <article>

            <ul>

                @{ 

                    foreach (Organisation org in organisations)

                    {

                        <li><a href="@p.Url?taxnumber=@org.TaxNumber">@org.Name</a>

                    </li>

                    }

                }

            </ul>

        </article>

    </section>

    Here's my controller, which inherits from Umbraco but I use my own actions and return my custom model. Each action maps to a template and a controller maps to a doctype.

     

     [PluginController("LuminousAcuity")]

        public class OrganisationController : RenderMvcController

        {

     

            X509Certificate2 _certificate;

            

            string _pFXSerialNo;

            string _consumerKey;

            string _userAgent;

     

            public OrganisationController()

            {

                ConfigHelper configHelper = new ConfigHelper("/");

                _pFXSerialNo = configHelper.GetConfigSetting("PFXCertStoreSerialNo");

                _consumerKey = configHelper.GetConfigSetting("ConsumerKey");

                _userAgent = configHelper.GetConfigSetting("UserAgent");

     

                _certificate = CertificateHelper.FindBySerialNumber(_pFXSerialNo);

            }

            //

            // GET: /Organisations/

            public ActionResult Organisations()

            {

                using (IOrganisationRepository<Organisation> r = new OrganisationRepository(_userAgent, _consumerKey, _certificate))

                {

                    IEnumerable<Organisation> organisations = r.GetAll();

                    return CurrentTemplate(organisations);

                }

            }

        }

  • Karl Svensson 9 posts 29 karma points
    Oct 26, 2014 @ 13:33
    Karl Svensson
    0

    Thanks, I will test it.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies