MVC Editor适用于复合模型类型吗?

发布于 2024-10-15 03:26:57 字数 403 浏览 1 评论 0原文

我想将委托或命令对象作为参数与模型对象一起发送给 EditorFor。我可以使用 viewdata 来发送它,但我非常喜欢它强类型。有什么合理的方法可以做到这一点吗?

这大致就是我希望能够在编辑器模板中执行的操作:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Pair<MyModelType, TheDelegate>>" %>

但是如何将我的委托添加到这样的表达式中?

<%= Html.EditorFor(m => m.MyModelTypeField, "ThatEditor") %>

I would like to send a delegate or a command object as argument to an EditorFor along with the model object. I could use viewdata to send it with, but I would very much like it strongly typed. Is there any reasonable way to do this?

This is roughly what I'd like to be able to do in an editor template:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Pair<MyModelType, TheDelegate>>" %>

But how could I add my delegate to an expression such as this?

<%= Html.EditorFor(m => m.MyModelTypeField, "ThatEditor") %>

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

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

发布评论

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

评论(1

窗影残 2024-10-22 03:26:57

除非您修改视图模型并让控制器传递必要的信息,否则您无法以强类型的方式执行此操作。那么

public class MyModelTypeWithDelegate
{
    public MyModelType MyModelType { get; set; }
    public TheDelegate MyModelTypeDelegate { get; set; }
}

public class MyViewModel
{
    public MyModelTypeWithDelegate MyModelTypeWithDelegate { get; set; }
}

<%= Html.EditorFor(m => m.MyModelTypeWithDelegate, "ThatEditor") %>

另一种可能性是将其作为附加视图数据传递,但不会强类型化:

<%= Html.EditorFor(m => m.MyModelTypeField, "ThatEditor", new { TheDelegate = someDelegate }) %>

然后在编辑器模板内:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyModelType>" %>
<%
    var del = (TheDelegate)ViewData["TheDelegate"];
%>

You can't do this in a strongly typed manner unless you modify your view model and have the controller pass the necessary information. So

public class MyModelTypeWithDelegate
{
    public MyModelType MyModelType { get; set; }
    public TheDelegate MyModelTypeDelegate { get; set; }
}

public class MyViewModel
{
    public MyModelTypeWithDelegate MyModelTypeWithDelegate { get; set; }
}

and then:

<%= Html.EditorFor(m => m.MyModelTypeWithDelegate, "ThatEditor") %>

The other possibility is to pass it as additional viewdata but it won't be strongly typed:

<%= Html.EditorFor(m => m.MyModelTypeField, "ThatEditor", new { TheDelegate = someDelegate }) %>

and then inside your editor template:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyModelType>" %>
<%
    var del = (TheDelegate)ViewData["TheDelegate"];
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文