Copied to clipboard

Flag this post as spam?

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


  • Roger Withnell 128 posts 613 karma points
    Jun 15, 2015 @ 13:44
    Roger Withnell
    0

    Passing a single value to an IEnumerable view

    With Umbraco 7, Visual Studio 2013 and MVC, I am using webGrid to display a list of links to documents, each with a date field.

    Accordingly, the data collected by the controller is a list and the View uses IEnumerable to display the collection in the webGrid.

    But I also need to pass a single value to the View (this value is the ID of the folder containing the documents). Because the View is IEnumerable, I can't work out how to get this single value.

    My code is as follows.

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
    
        namespace ParishMaster.Models
        {
            public class DocumentList
            {
                public string Document { get; set; }
                public DateTime Date { get; set; }
                public string Description { get; set; }
                public string Url { get; set; }
                public string PageName { get; set; } //  This is the single value property.
            }
        }
    
    
        using ParishMaster.Models;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using Umbraco.Web.Mvc;
    
        namespace ParishMaster.Controllers
        {
            public class DocumentListSurfaceController : SurfaceController
            {
                // GET: DocumentList
                public ActionResult Index()
                {
                    DocumentList vDocument = new DocumentList();
                    List<DocumentList> vDocumentList = new List<DocumentList>();
    
                    var page = Umbraco.Content(1096);
                    var vPageName = page.name;
                    vDocument.PageName = vPageName;  //This is the single value property
    
                    foreach (var child in page.Children)
                    {
                        vDocument = new DocumentList();
                        vDocument.Document = child.name;
                        vDocument.Date = child.dtpDocumentDate.Date;
                        vDocument.Description = child.dtpDocumentDescription;
                        vDocument.Url = Umbraco.Media(child.dtpDocumentFile).Url;
                        vDocumentList.Add(vDocument);
                    }
    
                    var data = vDocumentList;
    
                    return PartialView("DocumentList", data);
    
                }
            }
        }
    
    @model IEnumerable<ParishMaster.Models.DocumentList>
    
    @{
        WebGrid vGrid = new WebGrid(Model, defaultSort: "Date", rowsPerPage: 12);
    }
    
    <h3>
        @*How do I write the PageName property value here.*@
    </h3>
    
        @vGrid.GetHtml(
                alternatingRowStyle: "docList-alternating-row",
                tableStyle: "docList-table",
                headerStyle: "docList-header",
                footerStyle: "docList-footer",
                columns: new[] {
            vGrid.Column("Date", header: "Date"),
            vGrid.Column("Document", format: @<text><a href="@Href(item.Url)" target="_blank">@item.Document</a></text>),
        })
    

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 15, 2015 @ 13:59
    David Brendel
    0

    Hi Roger,

    think you can add your single value to the viewbag and in the view output it.

    ViewBag.MyValue = "Test" @ViewBag.MyValue

    That should work.

    Regards David

  • Maff 141 posts 465 karma points
    Jun 15, 2015 @ 14:59
    Maff
    0

    Hi Roger,

    Rather than adding the page.name to each DocumentList property in your List

    public class MyViewModel {
      public List<DocumentList> DocumentList { get; set; }
      public string PageName { get; set;}
    }
    

    Once you've built up your vDocumentList you can then add it all to the object:

    MyViewModel myViewModel = new MyViewModel {
      DocumentList = vDocumentList,
      PageName = page.name
    };
    

    and then pass that object back to your view:

    return PartialView("DocumentList", myViewModel);
    

    Thanks,

    Maff

  • Roger Withnell 128 posts 613 karma points
    Jun 19, 2015 @ 23:36
    Roger Withnell
    0

    Thanks, Maff.

    I tried to implement your solution but I am getting an error when the view is called.

    My view is IEnumerable:

    @model IEnumerable<ParishMaster.Models.DocumentList>
    

    Won't this give an error if I send to it:

            DocumentList myViewModel = new DocumentList
            {
                Documents = vDocumentList,
                PageName = page.name
            };
    
            return PartialView("DocumentList", myViewModel);
    

    as you suggest?

    Roger

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 20, 2015 @ 09:13
    David Brendel
    0

    Hi Roger,

    if you remove the IEnumerable in the view and just use @model yourModel then it should work.

    Regards David

  • Roger Withnell 128 posts 613 karma points
    Jun 20, 2015 @ 09:52
    Roger Withnell
    0

    Thank, David.

    If I remove the IEnumerable, the PageName works but the webGrid gives an error on the WebGrid vGrid = new WebGrid line:

    @model ParishMaster.Models.DocumentList
    
    <h3>
        @Model.PageName
    </h3>
    
    @{
        WebGrid vGrid = new WebGrid(Model, defaultSort: "Date", rowsPerPage: 12); //error on this line.
    
    }
    
        @vGrid.GetHtml(
                alternatingRowStyle: "docList-alternating-row",
                tableStyle: "docList-table",
                headerStyle: "docList-header",
                footerStyle: "docList-footer",
                columns: new[] {
            vGrid.Column("Date", header: "Date"),
            vGrid.Column("Document", format: @<text><a href="@Href(item.Url)" target="_blank">@item.Document</a></text>),
        })
    

    because the webGrid wants a collection and therefore need an IEnumerable?

    Roger

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 20, 2015 @ 19:16
    David Brendel
    100

    Hi Roger,

    think it is because you are using Model directly. Try to use Model.Documents instead. Think this should work.

    Regards David

Please Sign in or register to post replies

Write your reply to:

Draft