Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 嵌套编辑器模板:无法传递模型=>;模型到基础编辑器模板_C#_Asp.net Mvc_Inheritance_Mvc Editor Templates - Fatal编程技术网

C# 嵌套编辑器模板:无法传递模型=>;模型到基础编辑器模板

C# 嵌套编辑器模板:无法传递模型=>;模型到基础编辑器模板,c#,asp.net-mvc,inheritance,mvc-editor-templates,C#,Asp.net Mvc,Inheritance,Mvc Editor Templates,以以下模型为例: 名称空间MyNamespace.Model { //在本例中,为了简单起见,您只能有两个朋友:P 公课朋友{ 公众人物{get;set;} 公共Hipster HipsterFriend{get;set;} } 公共阶层人士{ 公共字符串名称{get;set;} 公共字符串PhoneNumber{get;set;} } 公务舱潮人:人 { 公共HatType HatPreference{get;set;} 公共int冷却点{get;set;} } } 假设我们正在尝试添加两个

以以下模型为例:

名称空间MyNamespace.Model
{
//在本例中,为了简单起见,您只能有两个朋友:P
公课朋友{
公众人物{get;set;}
公共Hipster HipsterFriend{get;set;}
}
公共阶层人士{
公共字符串名称{get;set;}
公共字符串PhoneNumber{get;set;}
}
公务舱潮人:人
{
公共HatType HatPreference{get;set;}
公共int冷却点{get;set;}
}
}
假设我们正在尝试添加两个朋友,一个普通朋友和一个时髦朋友:

创建.cshtml

@model MyNamespace.model.Friends
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.EditorFor(model=>model.NormalFriend)//按照约定使用Person EditorTemplate
@Html.EditorFor(model=>model.HipsterFriend)//按约定使用Hipster EditorTemplate
编辑模板/Person.cshtml

@model MyNamespace.model.Person
@EditorFor(model=>model.Name)
@EditorFor(model=>model.PhoneNumber)
编辑模板/Hipster.cshtml

@model MyNamespace.model.Hipster
@EditorFor(model=>model,“Person”)//model.CoolPoints)
@EditorFor(model=>model.HatPreference)
Hipster
EditorTemplate中,代码

@Html.EditorFor(model => model, "Person")
应该将
Hipster
模型传递给
Person
EditorTemplate,这样我就不必为共享属性(基本类型
Person
)重复模板化代码

目前,上述代码完全忽略了
EditorFor
,只显示了
Hipster
特定属性(即
CoolPoints
HatPreference

或者,我可以在Create.cshtml中更改以下代码:

@Html.EditorFor(model=>model.NormalFriend)
@EditorFor(model=>model.HipsterFriend)
致:

@Html.EditorFor(model=>model.NormalFriend)
@EditorFor(model=>model.HipsterFriend,“Person”)//打破惯例和嵌套思想
@EditorFor(model=>model.HipsterFriend)
这将实现我想要的,但在
Hipster
EditorTemplate中我无法做到这一点似乎是错误的

我的逻辑在这方面有缺陷还是遗漏了什么?我想可能是因为担心递归而忽略了相关行,但是
templateName
参数应该会澄清。这种继承基类型和嵌套的想法适用于partial,但partial不处理
ViewData.TemplateInfo.GetFullHtmlFieldNames



这个问题是相关的:虽然这个解决方案不适用于我的情况。是否有一种解决方法可以嵌套视图而不使用分部,或者这是我唯一的方法?

您能解释一下为什么不能在模板中使用分部吗?好的,分部不保留父属性信息。因此您将获得重复的
ID
名称s
将无法区分
NormalFriend
HipsterFriend
属性(即
EditorFor(model=>model.Name)
将返回类似
的内容,而不是