Copied to clipboard

Flag this post as spam?

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


  • Justin Pingatore 1 post 71 karma points
    Sep 24, 2019 @ 15:03
    Justin Pingatore
    0

    How do I render a strongly typed grid using mvc?

    I am trying to render a 'strongly-typed' grid in my mvc application but I am having trouble using GetGridHtml() when using @model in my view. Below is my work around for now but I'd like to keep everything in partial view if I can.

    ViewModel

    public class NewsDetailViewModel
    {
        public int NewsID { get; set; }
        public string Title { get; set; }
        public DateTime PublishedDate { get; set; }
    
        public NewsDetailViewModel(int newsId, string title, DateTime publishedDate)
        {
            NewsID = newsId;
            Title = title;
            PublishedDate = publishedDate;
        }
    }
    

    Surface Controller

        public ActionResult RenderNewsDetail()
        {
            var model = GetNewsDetailViewModel();
    
            return PartialView($"{PARTIAL_VIEW_FOLDER}_NewsDetail.cshtml", model);
        }
    
        private NewsDetailViewModel GetNewsDetailViewModel()
        {
            IPublishedContent newsDetail = CurrentPage.AncestorOrSelf("newsDetail");
    
            int newsID = newsDetail.Id;
            string title = newsDetail.Value<string>("title");
            var date = newsDetail.Value<DateTime>("publishedDate");
    
            NewsDetailViewModel model = new NewsDetailViewModel(newsID, title, date);
            return model;
        }
    

    Template

    @inherits UmbracoViewPage
    @{
        Layout = "master.cshtml";
    }
    
    @{ Html.RenderAction("RenderNewsDetail", "News");}
    <div class="main-content__body copy">
        @Html.GetGridHtml(Model, "content", "CustomGrid")
    </div>
    

    Partial

    @model Framework.UI.Models.NewsDetailViewModel
    @using Framework.UI.Models
    
    <div id="content">
        <h1>Model.Title</h1>
        <p>Model.PublishedDate</p>
    </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft