Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Apr 09, 2015 @ 12:48
    Craig O'Mahony
    0

    Displaying Data using MVC

    Hi folks,

    I understand that what I'm asking here might be outside what this forum is all about but I'm stuck (well I never really got started) and will try anything!!

    I've got an Umbraco v7.2.4 install and I'm trying to display some data on a template using MVC but I cannot get it to work. So apologies for the long winded post but I thought that I'd better give you everything.

    In my Models I've got:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace SxWhite.Models
    {
        public class CoursesViewModel
        {
            public int iNodeID { get; set; }
            public string CourseTitle { get; set; }
            public string CourseDesc { get; set; }
        }
    }

    In my controller: *Ultimately I need to populate this 'list' from my Content in Umbraco but that's another matter!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    namespace SxWhite.Controllers
    {
        public class CoursesController : SurfaceController
        {
            //
            // GET: /Courses/
            public ActionResult Index()
            {
                var courses = new List<CoursesViewModel>
                {
                    new CoursesViewModel {iNodeID=1, CourseDesc="Description 1", CourseTitle="Title 1"},
                    new CoursesViewModel {iNodeID=2, CourseDesc="Description 2", CourseTitle="Title 2"}
                };
                return PartialView("courses", new CoursesViewModel());
            }
        }
    }

    My view:

    @model SxWhite.Models.CoursesViewModel
    @Html.DisplayTextFor(m=> m.CourseDesc)
    @Html.DisplayFor(m=> m.CourseDesc)
    <h1>HELLO</h1>

    And in my template I'm calling it with:

    @Html.Action("Index", "Courses")   

    I'm not getting an error at all but nothing is being displayed on the page and I'm just going round in circles.

    Any help would be massively massively massively appreciated.

    thanks,

    Craig

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Apr 09, 2015 @ 13:46
    Ismail Mayat
    0

    Craig,

    You are passing through a new model and not the list of courses.  Pass through the courses also you will need to update your view to acccept List<CoursesViewModel> although a better of doing this is to hijack the Courses route see https://our.umbraco.org/Documentation/Reference/Mvc/custom-controllers

  • Craig O'Mahony 364 posts 918 karma points
    Apr 09, 2015 @ 14:26
    Craig O'Mahony
    0

    Hi Ismail,

    Thanks for your time.

    So your saying that I should change my controller to return PartialView("courses", courses); but I'm not sure what I need to do to my view to accept List<CoursesViewModel>.

    Do you mind just helping an idiot a little more please?

    Thanks,

    Craig.

    I've bookmarked that link and I'll read through a little later.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Apr 09, 2015 @ 14:40
    Ismail Mayat
    100

    Craig,

    In you view change 

    @modelSxWhite.Models.CoursesViewModel

    to

    @model IEumerable<SxWhite.Models.CoursesViewModel>

    then you will need to loop through your courses and write out data for each one.

    Regards

    Ismail

  • Craig O'Mahony 364 posts 918 karma points
    Apr 09, 2015 @ 15:01
    Craig O'Mahony
    0

    AWESOME!

    You are a star!

    Now all I gotta do is work for how to build the list using the content in my Umbraco build.....

    Thanks!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 09, 2015 @ 15:44
    Jeroen Breuer
    2

    Hello,

    If you want some MVC examples have a look at the Hybrid Framework.

    Jeroen

  • Craig O'Mahony 364 posts 918 karma points
    Apr 09, 2015 @ 15:52
    Craig O'Mahony
    0

    Cheer Jeroen,

    I shall have a butchers.

Please Sign in or register to post replies

Write your reply to:

Draft