views:

17

answers:

1

I've created a view for adding/removing items for a list. I'm trying to handle adding/removing dynamically, but I'm unclear on how to get the proper template context when the list is empty.

The view is based on:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service.Cms.ListItems;

namespace Bcp.Service.Cms.Documents
{
    public class Tasting //: Document
    {
        public Tasting() { }

        public List<DocumentListItem> relatedCrap { get; set; }
    }
}

Related List Item looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Service.Cms.ListItems
{
    public class DocumentListItem
    {
        public DocumentListItem() { }

        public Int32 documentId { get; set; }

        public String title { get; set; }

        public String documentType { get; set; }

        public String status { get; set; }
    }
}

In order to save the list items, each of the properties will need to be prefixed with tastingInfo.relatedDocs.#PROPERTYNAME# but if the list is empty when I load the view, I don't know how to access that context. Anybody have any ideas?

A: 

I ended up creating a Display Template for List<DocumentListItem> called DocumentListDisplay containing the following:

<input type="hidden" value="<%: ViewData.TemplateInfo.HtmlFieldPrefix %>" />

I just call this template in my View before calling EditorFor() on the same model. It's kludgey, but it provides the template context without having to hard code anything or add anything to ViewData.

Mathletics