MVC EditorFor 位于另一个 EditorFor 内

发布于 2024-09-29 00:34:25 字数 494 浏览 3 评论 0原文

我有一个模型角色的 EditorFor 模板,如下所示。我还有 EditorFor for Date,当我直接从 View 使用 EditorFor 时,它工作正常,但当我在编辑器内使用 EditoFor 时,它不起作用。有什么想法吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思碎 2024-10-06 00:34:26

这对我有用。

模型:

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

控制器:

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

视图 (~/Views/Home/Index.aspx):

<%: Html.EditorForModel() %>

MyViewModel 的编辑器模板 (~/Views/Home/EditorTemplates/MyViewModel.ascx):

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

编辑器日期时间模板 (~/Views/Home/EditorTemplates/DateTime.ascx):

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

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>
千仐 2024-10-06 00:34:25

对我来说也不起作用;我认为这是某种反递归保护。

如果将对“EditorFor”的外部调用更改为“Partial” - 即使指向同一个 .cshtml 文件 - 内部的“EditorFor”也将起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文