views:

219

answers:

1

It seems like it's not possible to edit custom object anymore after I upgraded to asp.net mvc 2 rc 2? I use this approach http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html with a custom object like this:

My model has just one property but inherits from an abstract base class public class Page : ContentItem { [DataType(DataType.MultilineText)] public virtual string MainIntro { get;set; } // This property render correct

    [DisplayFormat(NullDisplayText="(null value)")]
    public virtual DetailCollection Tags { get; set; }

}

My controller looks like this

    public ActionResult Edit(string pagePath) {
        var page = _repository.GetByUrlSegment(pagePath);
        return View(page.EditViewName, new DashboardModel(page, RootPages));
    }

And my view looks like this

<% using (Html.BeginForm("update","Dashboard", FormMethod.Post, new { name = "editForm" } )) %>
<% { %>
    <div>
        <%=Html.EditorFor(model => model.CurrentItem) %>
        <div class="editor-button">
            <input type="submit" value="Save" />
        </div>
    </div>            
<% } %>
+1  A: 

Perhaps it would be better to expose this to the view as a space-separated string and exclude the collection from being displayed in the view. Alternatively, you might be able to define a specific template for how you want to display a collection. It's not clear to me how MVC would be able to determine what to display otherwise.

Try something like:

        [ShowForDisplay(false)]
        [ShowForEdit(false)]
        public virtual DetailCollection Tags { get; set; }

        public virtual string TagList
        {
             get
             {
                 if (tags == null) return "(null value)";

                 // assumes DetailCollection implements IEnumerable<string>
                 return string.Join( " ", tags.Select( t => t).ToArray() );
             }
             set
             {
                  tags = new DetailCollection( value.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries )
                                                    .Select( s => s.Trim() ) );
             }
         }
tvanfosson
I was thinking about using the UIHint attribute to specify which template to use.
Marcus