- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个实体模型,它包含消息对象的集合,这些消息对象属于 Message 类型,具有多个属性,包括内容、MessageID、from 和 to。
我已经为 Message 类型创建了一个 EditorTemplate,但是,我无法让它显示 Messages 集合的内容。
没有错误,但没有输出。
请注意, View 代码来自父 Talkback 类的 EditorTemplate。您可以让一个 EditorTemplate 为子集合调用另一个 EditorTemplate 吗?
Talkback 和 Message 类都是由 Entity 框架从现有数据库生成的。
查看代码:
<% foreach (TalkbackEntityTest.Message msg in Model.Messages)
{
Html.EditorFor(x=> msg, "Message");
} %>
这是我的模板代码。它是标准的自动生成的 View 代码,有一些小的变化。
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TalkbackEntityTest.Message>" %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.MessageID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.MessageID) %>
<%: Html.ValidationMessageFor(model => model.MessageID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.acad_period) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.acad_period) %>
<%: Html.ValidationMessageFor(model => model.acad_period) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.talkback_id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.talkback_id) %>
<%: Html.ValidationMessageFor(model => model.talkback_id) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.From) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.From) %>
<%: Html.ValidationMessageFor(model => model.From) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.To) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.To) %>
<%: Html.ValidationMessageFor(model => model.To) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.SentDatetime) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.SentDatetime, String.Format("{0:g}", Model.SentDatetime)) %>
<%: Html.ValidationMessageFor(model => model.SentDatetime) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.content) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.content) %>
<%: Html.ValidationMessageFor(model => model.content) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.MessageTypeID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.MessageTypeID) %>
<%: Html.ValidationMessageFor(model => model.MessageTypeID) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
Message 集合中肯定有内容,因为如果我删除 EditorFor 并将 response.write 放在 Message 类的 content 属性上,我会在页面上获得 3 个 Message 对象的内容字段,这与预期完全一样.
最佳答案
您不需要手动foreach
。只需将包含您在 ~/Shared/EditorTemplates/
文件夹中显示的编辑器模板的名为 Message.ascx
的文件放入您的 View 中即可:
<%: Html.EditorFor(model => model.Messages) %>
关于c# - ForEach 与 EditorFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2798700/
我想获取我的项目名称的值,但警报结果始终是: Value: function(value){ return access(this, function(value){ return value ===
我有一个用于模型角色的 EditorFor 模板,如下所示。我也有用于 Date 的 EditorFor,当我直接从 View 使用 EditorFor 时它工作正常,但是当我在编辑器中使用 Edit
我有一个像这样的 EditorFor HTML 助手: @Html.EditorFor(m => m.Name, belowLevel ? disabledHtmlOptions : null) -
我的页面中有第二件事: @for (int i = 0; i @Html.EditorFor(m => Model.Option[i].Date, n
我在将 css 类添加到 HTML.EditorFor() 时遇到问题。如果有人有同样的问题,我正在网上搜索,但我找不到任何东西。 你能帮帮我吗?因此,未定义类不是 css 的问题,而是简单地说,它没
我想在我的应用程序中自动聚焦于 editorfor,但我似乎做不到。我已在文本框上成功使用自动对焦,但我想使用编辑器来保持我的应用程序的外观通用。 任何对此的解决方案将不胜感激,谢谢。 我的尝试: @
我希望我的复选框在未选中时禁用 EditorsFor,在选中时启用它们。我知道我必须使用JavaScript,我在网上找到了一些代码,但似乎它们不起作用。 这是我的查看代码: @{Html.Rende
在我看来,我有 4 个 EditorFor 字段,我想将它们设置为 DatePickers @Html.EditorFor(model => model.Date1)
我一直在尝试使用此代码在我的 EditorFor 上设置最小值和最大值: From Date: @Html.EditorFor(model => model.SelectedYearBegin, ne
假设你有一个像这样的模型: class Foo { public int A {get; set;} public int B {get; set;} } class SomeMode
到目前为止,我们可以使用 Html.EditorFor() 为数据类型动态呈现适当的模板 - 例如string、int 或自定义类型,例如“地址”。 现在我想使用 EditorFor() 来呈现一个“
昨天,经过大量测试,我得到了以下结果,可以根据 ViewBag.CanEdit 的值有选择地将 readonly 属性应用于控件; @Html.EditorFor(m => m.Location, n
使用 C# 绑定(bind)到 MVC3 中的 View 模型。 难以设置文本框的初始值,尝试在不必创建模板等的情况下这样做 有什么想法或者我必须沿着创建自定义模板的道路走下去吗? 谢谢,蒂姆 最佳答
您好,我为价格定义了一个小数字段,如下所示: [Required(ErrorMessage = "Asking Price/Rent is required.")] [Display(Name = "
这是我的 View 模型: [Required(ErrorMessageResourceType=typeof(Resources.ValidationsResources), E
有没有人写过或有办法使用属性名称而不是表达式来调用 EditorFor。 EditorFor("Code") 相对于 EditorFor(_ => _.Code) 我想写一些更通用的东西。通过创建表达
我有一个实体模型,它包含消息对象的集合,这些消息对象属于 Message 类型,具有多个属性,包括内容、MessageID、from 和 to。 我已经为 Message 类型创建了一个 Editor
我有一个具有以下属性的模型: [Required] [HiddenInput(DisplayValue = false)] public override int Id { get; set;} 现在
有没有办法为 Html.EditorFor 助手定义一个全局模板? 我想改变输出的标记,例如而不是渲染 它将呈现: 这适用于当我将 Html.EditorF
嗨,我对网页设计和语言(包括 JavaScript)还很陌生。 在强类型 cshtml View 中,是否可以仅显示基于复选框的文本字段(其中文本字段和复选框都对模型强类型化?)我创建了一个示例场景:
我是一名优秀的程序员,十分优秀!