I implemented what you have and was able to reproduce it. I set a breakpoint in Object.cshtml so that I can inspect it, and I was taken by surprise to realize that it didn't even hit the object template when using the field set template. Then I went through the fieldset template and saw that it just calls the template, so something should happen in the code that prevents it from displaying the object template.
I opened the source code of MVC3 , searched for EditorForModel and found the correct function.
public static MvcHtmlString EditorForModel(this HtmlHelper html) { return MvcHtmlString.Create(TemplateHelpers.TemplateHelper(html, html.ViewData.ModelMetadata, String.Empty, null , DataBoundControlMode.Edit, null )); }
Obviously, this was not the case, so I pressed F12 on TemplateHelpers.TemplateHelper , and as soon as I pressed F12 again on a single-line call, which brings you to the meat function. Here I found this short code code starting at line 214 of TemplateHelpers.cs :
These comments are actually in the code, and here we have the answer to your question: Can one model be transmitted through several editor templates? , the answer is no * .
So this seems like a very reasonable option for such a feature, so finding an alternative is probably worth it. I suspected that the razor template delegate would solve this packing function, so I tried it.
@{ Func<dynamic, object> fieldset = @<fieldset><legend>@ViewData.ModelMetadata.DisplayName</legend>@Html.EditorForModel()</fieldset>; } @using (Html.BeginForm()) { //@Html.EditorForModel("Fieldset") //@Html.EditorForModel() @fieldset(Model) }
And viola! It worked! I will leave it for you to implement this as a way to expand (and much more reusable). Here's a short blog post about razor template delegates .
* Technically, you can rewrite this function and compile your version of MVC3, but this is probably more of a problem than it's worth. We tried to do this in the careers project when we found out that the Html.ActionLink function Html.ActionLink pretty slow when you have several hundred routes defined. There is a problem with signing with the rest of the libraries, which we decided were not worth our time to work now and support future releases of MVC.