Copied to clipboard

Flag this post as spam?

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


  • Casper Andersen 126 posts 508 karma points
    Feb 18, 2015 @ 13:57
    Casper Andersen
    0

    Getting custom Model, Controller and View to interact

    Hi there,i am new to Umbraco and also new to MVC, i have good knowledge of Web Forms but i want to try and learn MVC together with Umbraco. My problem is that i start by going to Umbraco backoffice and creating a document type i call Events, this i make into a List View, and then i make another document type with a matching template i call Event after this i create a Controller in this controller i want to display each created Event that i create in the backoffice of Umbraco, an Event contains 4 things Name (textstring) Description (multilinetextstring) Location (textstring) Image (medipicker).

    So basically i want it so everytime a create a new Event in the Events list view i want make another instance of the Event template, so i can have like 10 events on the home page i have, then when this is all good and done i also want it to be able so that when i click on the event which is a box with the name short description and an image, i want it to link to a new page where the event is shown as a full page so to speak. I am pretty sure i know how to do the last part with @Html.ActionLink and then use a method in my controller to handle this but other than that i feel lost, i have seem all the Videos on Umbraco.tv and i have read countless forum posts the last 3 days so any help would be very much appreciated :)

  • Casper Andersen 126 posts 508 karma points
    Feb 19, 2015 @ 02:28
    Casper Andersen
    0

    Funny enough i finally got it working by myself, used a foreach loop to loop through all the Children of the parent node and then printed out their custom fields my code looks like this if anyone in the futre needs to get it to work,

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @using Giantgrandprix.App_Code

    @foreach (var page in Model.Content.Children.Where(x => x.DocumentTypeAlias == "Event"))

    {

        var img = Umbraco.TypedMedia(page.GetPropertyValue<string>("image", recurse: true));

        <a href="/events/[email protected]" class="linkEvent">

            <div class="customEvent">

                <div class="thumbnail">

                    <img src="@img.Url" alt="@img.Name">

                    <div class="caption">

                        <h3>@page.GetPropertyValue("name")</h3>

                        <p>@Html.ShortText(page.GetPropertyValue("description").ToString(),25)</p>

                    </div>

                </div>

            </div>

        </a>

    }

     

    My only problem now is that i dont know how to make this work using a Controller so that i get this code to run as an Index method and then of course when i click on the link i want to pass on an Id of what i clicked on so that it hides the eents and shows the single event

  • Casper Andersen 126 posts 508 karma points
    Feb 21, 2015 @ 13:47
    Casper Andersen
    100

    Got everything to work now moved my for each loop out of the partial view and out to where i call the Controller action and supply it with values so now the page looks like so

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @using Giantgrandprix.App_Code

    @{

        Layout = "~/Views/Shared/Master.cshtml";

    }

     

    <div class="col-lg-12">

        <div class="row eventWrapper">

            @foreach (var page in Model.Content.DescendantsOrSelf("events").FirstOrDefault().Children.Take(10))

            {

                var img = Umbraco.TypedMedia(page.GetPropertyValue("image", true));

                @Html.Action("Index", "EventSurface", new { id = page.Id, name = page.GetPropertyValue("name"), description = page.GetPropertyValue("description"), image = img.Url })

            }

        </div>

    </div>

    <div class="col-lg-12" style="padding: 0px;">

        <div id="facebook_sidebar" class="col-lg-2 col-md-12 col-sm-12 col-xs-12 pull-left"><h2>Facebook</h2></div>

        <article id="article_master" class="col-lg-6 col-md-12 col-sm-12 col-xs-12 col-lg-offset-1"><h2>Main Content</h2>

        </article>

        <div id="sponsor_sidebar" class="col-lg-2 col-md-12 col-sm-12 col-xs-12 pull-right"><h2>Sponsorer</h2></div>

    </div> 

    Because on the view page i need access to the model.content but if i pass in the values to my controller then i dont need the Model.Content on my Partial View pages so nor my Partiel view looks like this

    @model Giantgrandprix.Models.EventModel

    @using Giantgrandprix.App_Code

     

        @{/*var img = Umbraco.TypedMedia(page.GetPropertyValue<string>("image", recurse: true));*/}

        <a href="/event/@Html.DisplayTextFor(x => x.event_id)" class="linkEvent">

            <div class="customEvent">

                <div class="thumbnail">

                    <img src="@Html.DisplayFor(x => x.event_image)" alt="@Html.DisplayTextFor(x => x.event_image)">

                    <div class="caption">

                        <h3>@Html.DisplayTextFor(x => x.event_name)</h3>

                        <p>@Html.ShortText(Html.DisplayTextFor(x => x.event_description).ToString(),20)</p>

                    </div>

                </div>

            </div>

        </a> 

Please Sign in or register to post replies

Write your reply to:

Draft