开发者

Html.EditorFor does not render anything if I use a custom object?

开发者 https://www.devze.com 2022-12-20 16:21 出处:网络
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-cust

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 (Ht开发者_如何学运维ml.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>            
<% } %>


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() ) );
             }
         }
0

精彩评论

暂无评论...
验证码 换一张
取 消