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>),
})
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.
Your help would be much appreciated.
Thanking you in anticipation.
Roger
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
Hi Roger,
Rather than adding the page.name to each DocumentList property in your List
Once you've built up your vDocumentList you can then add it all to the object:
and then pass that object back to your view:
Thanks,
Maff
Thanks, Maff.
I tried to implement your solution but I am getting an error when the view is called.
My view is IEnumerable:
Won't this give an error if I send to it:
as you suggest?
Roger
Hi Roger,
if you remove the IEnumerable in the view and just use @model yourModel then it should work.
Regards David
Thank, David.
If I remove the IEnumerable, the PageName works but the webGrid gives an error on the WebGrid vGrid = new WebGrid line:
because the webGrid wants a collection and therefore need an IEnumerable?
Roger
Hi Roger,
think it is because you are using Model directly. Try to use Model.Documents instead. Think this should work.
Regards David
is working on a reply...