Mathew
Mathew

Reputation:

MVC EditorFor inside Another EditorFor

I have an EditorFor Template for a Model Role as below. I also have EditorFor for Date which works fine when I use EditorFor directly from View but when I have EditoFor inside an editor for it doesn't work. Any idea?

Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl[ucsrManagementSystem.Models.ContactsInMailingListsViewModel]"

Html.EditorFor(m => m.IsInMainlingList)  
Html.EditorFor(m => m.Id)  
Html.EditorFor(m => m.Name)  
Html.EditorFor(m => m.EndDate)//This is not showing Date's Editor Template when inside another EditorFor

Upvotes: 5

Views: 1796

Answers (2)

pete the pagan-gerbil
pete the pagan-gerbil

Reputation: 3166

It doesn't work for me either; I'm presuming that it's some kind of anti-recursion protection.

If you change the outer call to 'EditorFor' to a 'Partial' instead - even pointing at the same .cshtml file - the inner 'EditorFor's will work.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

It works for me.

Model:

public class MyViewModel
{
    public DateTime Date { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            Date = DateTime.Now
        });
    }
}

View (~/Views/Home/Index.aspx):

<%: Html.EditorForModel() %>

Editor template for MyViewModel (~/Views/Home/EditorTemplates/MyViewModel.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.MyViewModel>" %>
<%: Html.EditorFor(x => x.Date) %>

Editor template for DateTime (~/Views/Home/EditorTemplates/DateTime.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<div>Some markup to edit date</div>

Upvotes: 0

Related Questions