如何更改HtmlFieldPrefix的索引?
我是Children[0]从EditorFor()获得的,我想制作它Children[@Model.Id]
或者Children[2].Children[4]来自EditorFor()而我想制作它Children[@"ParentModel".Id].Children[@Model.Id]
直到运行时我才会知道实际的前缀.最好有内置的方法来改变它吗?
或者只是弄乱字符串?我还是C#字符串函数的新手.
这是MVC3 Razor httppost返回复杂对象子集合的后续问题.
我给出的例子非常简单.子集合实际上是一个对象集合,它们都来自抽象基类.所以集合有一个基类列表.
我已经为每个派生类创建了一个模板,并尝试使用if子类型,然后将模板名称作为字符串.模板将呈现给视图,但不会在帖子后面填充.
我不确定如何使用编辑器对模板进行选择正确的模板,并将信息编组回父容器中的子对象.
这可能很容易,但我已经搜索了 SO 并为此尝试了一些建议,但没有一个可行。我正在使用一个 MVC 编辑器模板,它有一个带有标准 html 按钮和其他字段的 div。当我将一个集合传递给模板时,它将使用唯一的 ID 呈现集合中每个项目的字段。然后我想在单击任何按钮时打开一个对话框。按钮在编辑器模板中呈现如下:
@model ProductViewModel
<span>
<button id="btnSelectTags-@(ViewData.TemplateInfo.HtmlFieldPrefix)" class="sig-button ie-shadowed select-tag" type="button" title="Select tags..." style="margin-right: 10px">Select tags</button>
</span>
// other fields
Run Code Online (Sandbox Code Playgroud)
因此,如果我将一个包含 2 个对象的集合传递给编辑器模板,我会得到以下 html:
<button title="Select tags..." class="sig-button ie-shadowed select-tag" id="btnSelectTags-Products[0]" style="margin-right: 10px;" type="button">
// other fields then next item:
<button title="Select tags..." class="sig-button ie-shadowed select-tag" id="btnSelectTags-Products[1]" style="margin-right: 10px;" type="button">
Run Code Online (Sandbox Code Playgroud)
这看起来不错,并为每个按钮提供了唯一的 ID。他们需要有一个唯一的 id(我认为),因为每个 div 中的项目都可以有自己的一组标签。因此,我想使用此 jQuery 向每个将打开对话框的按钮添加一个单击事件(我也尝试在选择器中包含其他类并尝试不使用“按钮”):
if ($("button.select-tag")) {
$(".select-tag").click(function () {
showTagDialogBox();
});
}
Run Code Online (Sandbox Code Playgroud)
这是标签被渲染的 div:
<div style="display: none">
<div …Run Code Online (Sandbox Code Playgroud) 我以为Html.HiddenFor可以使用像Html.DisplayFor或的模板Html.EditorFor.不幸的是,该方法不接受TemplateName其他方法.
我知道,解决方法是使用具有HiddenFors的DisplayFor/EditorFor模板.但我想了解如何扩展该Html.HiddenFor方法.任何人?
问候
我有一个剑道网格设置如下:
@(Html.Kendo().Grid<ParticipatingDentalEE>()
.Name("DentalEE")
.Columns(columns =>
{
columns.Bound(p => p.State).Title("State").Width(150).EditorTemplateName("State");
columns.Bound(p => p.Count).Title("Count").Width(150);
columns.Command(c => { c.Edit(); c.Destroy(); });
})
.DataSource(dataSource => dataSource
.Ajax()
.Model(m => {
m.Id(p => p.State);
m.Field(p => p.State).Editable(true);
m.Field(p => p.Count).Editable(true).DefaultValue("");
})
.Create(update => update.Action("EditingInline_Create", "Dental"))
.Read(read => read.Action("EditingInline_Read", "Dental"))
.Update(update => update.Action("EditingInline_Update", "Dental"))
.Destroy(update => update.Action("EditingInline_Destroy", "Dental"))
)
//.Scrollable()
//.Sortable()
.Editable(e => e.Mode(GridEditMode.InLine))
Run Code Online (Sandbox Code Playgroud)
)
"状态"列包含一个下拉模板,如下所示:
@(Html.Kendo().DropDownList()
.Name("States") // Name of the widget should be the same as the name of the property
.DataValueField("CODE") // The …Run Code Online (Sandbox Code Playgroud) asp.net-mvc mvc-editor-templates razor drop-down-menu kendo-grid
以下列模型为例:
namespace MyNamespace.Model
{
//you can only have two friends for the sake of simplicity in this example :P
public class Friends{
public Person NormalFriend { get; set; }
public Hipster HipsterFriend { get; set; }
}
public class Person{
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
public class Hipster : Person
{
public HatType HatPreference { get; set; }
public int CoolPoints { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
假设我们正在尝试添加我们的两个朋友,一个普通朋友和一个时髦朋友:
Create.cshtml: …
假设一个这样的抽象模型:
public abstract class MyClass : BaseEntity
{
[UIHint("File")]
public long? DocumentFileId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
问题是Cannot resolve template 'File',虽然View编辑器模板中有File.cshtml.

关键是,如果我没有将MyClass定义为抽象类,则会解决错误.
我的问题是,为什么编辑器模板无法在抽象类中解析,我该如何处理呢?
c# resharper abstract-class mvc-editor-templates asp.net-mvc-uihint
我有一个简单的模型:
public abstract class Person
{
[Required(ErrorMessage = "Il nome è obbligatorio!!!")]
[UIHint("txtGeneric")]
[Display(Name = "Nome")]
public string Name { get; set; }
[Required(ErrorMessage = "Il cognome è obbligatorio!!!")]
[UIHint("txtGeneric")]
[Display(Name = "Cognome")]
public string Surname { get; set; }
[Required(ErrorMessage = "L'email è obbligatoria e deve essere in formato valido (nome@dominio.it)!!!")]
[UIHint("txtEmail")]
[Display(Name = "E-mail")]
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在EditorTemplate里面创建了txtGeneric.cshtml文件.它是这样的:
@model string
<input name="@ViewData.TemplateInfo.HtmlFieldPrefix" id="@ViewData.TemplateInfo.HtmlFieldPrefix" data-validation="required" data-validation-error-msg="MESSAGE_TO_PUT" value="@Model" />
Run Code Online (Sandbox Code Playgroud)
我想知道如何将与Errormessage of Required属性相关联的文本放入我的txtGeneric.cshtml文件中.我怎样才能做到这一点?
谢谢
EditorTemplates很棒,因为它们在剃刀视图中允许某种"多态".但我错过了一个"砖"来完成多态支持:
一个特殊类型的EditorTemplate可以从EditorTemplate继承一般类型吗?
长版:
特定
class SpecialChild : GeneralChild { }
class Parent
{
GeneralChild AGeneralChild { get; set; }
SpecialChild ASpecialChild { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和两个编辑器模板
@* GeneralChild.cshtml *@
@model GeneralChild
<span>GeneralChild</span>
@* SpecialChild.cshtml *@
@model SpecialChild
<span>SpecialChild is a</span> <span>GeneralChild</span>
Run Code Online (Sandbox Code Playgroud)
我得到的(这就是我称之为"多态性"的原因)是:
@* Index.cshtml *@
@Html.EditorFor(m => m.AGeneralChild)
// prints "<span>GeneralChild</span>", however
@Html.EditorFor(m => m.ASpecialChild)
// prints "<span>SpecialChild is a</span> <span>GeneralChild</span>"
Run Code Online (Sandbox Code Playgroud)
也就是说,即使SpecialChild是GeneralChild并且有一个GeneralChild模板,它也会自动选择SpecialChild.cshtml模板.此外,如果我删除该模板,它将回退到GeneralChild.cshtml模板.换句话说,可以重用一般模板或在必要时覆盖它.
现在我想要的是:
我想重用GeneralChild.cshtml模板来定义SpecialChild.cshtml模板,就像在C#中调用"基本方法"一样.在伪代码中:
@* SpecialChild.cshtml *@
baseEditorFor()
@section SpecificPart
{
<span>SpecialChild is a </span>
}
@* …Run Code Online (Sandbox Code Playgroud) 我正在构建一个编辑器模板,它将接受htmlAttributes对象。
例子很简单。
在主视图中,我有类似的东西
@Html.EditorFor(Function(x) x.SomeProperty, New With {.htmlAttributes = New With {.class = "form-control"}}).
Run Code Online (Sandbox Code Playgroud)
在编辑器模板中,我需要class="form-control"编写属性。
如果在模板中我使用了类似的东西@Html.TextBoxFor(Function(x) x, ViewData("htmlAttributes")),那么一切都很好。但是如果我手动构建一个标签,我没有办法输出htmlAttributes,即我构建<input type="text" {htmlAttributes should be here} />
是否有任何公共方法可以正确呈现标签内的 HTML 属性?