Copied to clipboard

Flag this post as spam?

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


  • TomD 11 posts 103 karma points
    Dec 12, 2015 @ 10:51
    TomD
    0

    Get all nodes by specific document type and forward to an external page in new tab

    Hi all,

    I’m just trying to get all nodes by a specific document type in my View and my problem is as follows:

    Every time, when somebody clicks on a node, he should be forwarded to an external page in a new Tab. Up to this point, everything works fine. But when I use a BeginUmbracoForm because I need a function from my Surface Controller, one of them no longer works. Either the forwarding to the external page or the ActionResult in the controller.

    Furthermore I have the Problem, that my model (BannerModel), which I pass to my Controller, is not detected in my view. Which may be due to the fact that I use @Html.Partial ("SiteBanner", @Model.Content) instead of @Html.Action ("Index", "BannerSurface") in my template. But I have to do this because otherwise I’m not able to get the node of the document type in this way.

    Here my code for a better understanding:

    My Template, with whom I call the PartialView: SiteBanner.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @using ClientDependency.Core.Mvc;
    
    @{
        Layout = "Master.cshtml";
    }
    
    @section head { }
    
    <div class="container">
    
       @* Html.Action("Index", "BannerSurface")*@
    
       @Html.Partial("SiteBanner", @Model.Content)
    
    </div>
    

    My View, with whom I call the ActionResult: “Count” in my SurfaceController

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @using System;
    @using System.Collections;
    @using System.Linq;
    @using System.Web;
    @using Umbraco714.Models;
    @using Umbraco714.Repositories;
    @using Umbraco.Core;
    @using Umbraco.Core.Persistence;
    @using Umbraco.Core.Services;
    @using Umbraco.Web.Security;
    @using Umbraco.Web;
    @using Umbraco.Web.Models;
    @using Umbraco.Core.Models;
    
    
    @{
        var list = Model.AncestorOrSelf().Descendants("Bannercategory");
        var model = new BannerModel();
    }
    
    
     <ul style="list-style-type: none; ">
    
        @{
            foreach (var cat in list)
            {
                var bannerList = cat.Descendants("Banner");
                if (cat.Name == "Seitenrand")
                {
                    foreach (var banner in bannerList)
                    {
                        var img = Umbraco.Media(@banner.GetPropertyValue("bannerImg"));
                        var crop = img.GetCropUrl("umbracoFile", "NormalBanner");
    
                        <li>
                            @using (Html.BeginUmbracoForm<Umbraco714.Controller.BannerSurfaceController>("Counter"))
                            {
                                //<form action="@banner.GetPropertyValue("bannerRef")" target="_blank">
                                <button type="submit">
                                    <img src="@crop" />
    
                                    <h4>@banner.GetPropertyValue("bannerTitle")</h4>
                                    <p>@banner.GetPropertyValue("bannerText")</p>
                                </button>
                                //</form>
                            }
                            <input id="BannerId" name="BannerId" type="hidden" value="@banner.Id" />
                            <input id="BannerUrl" name="BannerUrl" type="hidden" value="@banner.GetPropertyValue("bannerRef")" />
                        </li>
    
    
                    }
                }
            }
        }
    
    </ul>
    

    My SurfaceController, with whom I increment the count of clicks and update the document type property: Clicks

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using Umbraco714.Models;
    using Umbraco714.Repositories;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using System.Diagnostics;
    
    
    namespace Umbraco714.Controller
    {
        public class BannerSurfaceController : SurfaceController
        {
    
            public ActionResult Index()
            {
                return PartialView("SiteBanner", new BannerModel());
            }
    
            public ActionResult Counter(BannerModel model)
            {
                var contentService = ApplicationContext.Current.Services.ContentService;
                var banner = contentService.GetById(Convert.ToInt32(model.BannerId));
    
                var count = Convert.ToInt32(banner.Properties["clicks"].Value.ToString()) + 1;
    
                banner.SetValue("clicks", count);
                contentService.SaveAndPublishWithStatus(banner);
    
                return Redirect(model.BannerUrl);
            }
        }
    }
    

    My Model, with whom I will get the BannerId and the BannerUrl

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Umbraco714.Models
    {
        public class BannerModel
        {
            public int Clicks { get; set; }
            public int BannerId { get; set; }
            public string BannerUrl { get; set; }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft