在 Orchard CMS 中创建自定义字段以从单独的模块呈现下拉列表
Creating Custom Field in Orchard CMS to render Dropdown from a separate module
我正在使用 OrchardCMS 创建网站,在这里我想要一个自定义表单的下拉菜单。
我创建了一个模块,我对其进行了 ajax 调用,它的 returns 下拉列表中填充了数据库获取的值。
我只需要以自定义形式使用此下拉列表。我正在弄清楚如何实现它?
我已经尝试使用此 link http://docs.orchardproject.net/Documentation/Creating-a-custom-field-type 创建自定义字段,但我仍然找不到自己的位置。应该有一些方法可以做到这一点。
请指导我如何去做。感谢您的回复。
谢谢
索海布
您只需为此创建一个字段即可。
MyModule/Fields/MyCustomField.cs:
public class MyCustomField : ContentField {
public string SelectedValue {
get { return Storage.Get<string>(); }
set { Storage.Set(value); }
}
}
MyModule/Drivers/MyCustomFieldDriver.cs:
public class MyCustomFieldDriver : ContentFieldDriver<MyCustomField> {
// EditorTemplates/Fields/MyCustom.cshtml
private const string TemplateName = "Fields/MyCustom";
private static string GetPrefix(ContentField field, ContentPart part) {
// handles spaces in field names
return (part.PartDefinition.Name + "." + field.Name)
.Replace(" ", "_");
}
protected override DriverResult Display(ContentPart part, MyCustomField field, string displayType, dynamic shapeHelper) {
return ContentShape("Fields_MyCustom",
field.Name,
f => f.Name(field.Name)
.SelectedValue(field.SelectedValue));
}
protected override DriverResult Editor(ContentPart part, MyCustomField field, dynamic shapeHelper) {
return ContentShape("Fields_MyCustom_Edit", () => shapeHelper.EditorTemplate(
TemplateName: TemplateName,
Model: field,
Prefix: GetPrefix(field, part)));
}
protected override DriverResult Editor(ContentPart part, MyCustomField field, IUpdateModel updater, dynamic shapeHelper) {
updater.TryUpdateModel(field, GetPrefix(field, part), null, null);
return Editor(part, field, shapeHelper);
}
}
MyModule/Views/Fields/MyCustom.cshtml:
@{
var selectedValue = Model.SelectedValue;
}
<h1>@selectedValue</h1>
MyModule/Views/EditorTemplates/Fields/MyCustom.cshtml:
@model MyModule.Fields.MyCustomField
<select id="@Html.IdFor(m => m.SelectedValue)" name="@Html.NameFor(m => m.SelectedValue)"></select>
@using (Script.Foot()) {
Script.Require("jQuery");
<script>
$(function () {
// your own url ofcourse
var url = 'http://jsonplaceholder.typicode.com/users',
dd = $("#@Html.IdFor(m => m.SelectedValue)");
$.getJSON(url, function (data) {
$.each(data, function () {
dd.append("<option value='" + this.name + "'>" + this.name + "</option>");
});
});
});
</script>
}
MyModule/Placement.信息:
<Placement>
<Place Fields_MyCustom_Edit="Content:3" />
<Place Fields_MyCustom="Content:3" />
</Placement>
这就是全部:)
我正在使用 OrchardCMS 创建网站,在这里我想要一个自定义表单的下拉菜单。 我创建了一个模块,我对其进行了 ajax 调用,它的 returns 下拉列表中填充了数据库获取的值。 我只需要以自定义形式使用此下拉列表。我正在弄清楚如何实现它? 我已经尝试使用此 link http://docs.orchardproject.net/Documentation/Creating-a-custom-field-type 创建自定义字段,但我仍然找不到自己的位置。应该有一些方法可以做到这一点。 请指导我如何去做。感谢您的回复。 谢谢 索海布
您只需为此创建一个字段即可。
MyModule/Fields/MyCustomField.cs:
public class MyCustomField : ContentField {
public string SelectedValue {
get { return Storage.Get<string>(); }
set { Storage.Set(value); }
}
}
MyModule/Drivers/MyCustomFieldDriver.cs:
public class MyCustomFieldDriver : ContentFieldDriver<MyCustomField> {
// EditorTemplates/Fields/MyCustom.cshtml
private const string TemplateName = "Fields/MyCustom";
private static string GetPrefix(ContentField field, ContentPart part) {
// handles spaces in field names
return (part.PartDefinition.Name + "." + field.Name)
.Replace(" ", "_");
}
protected override DriverResult Display(ContentPart part, MyCustomField field, string displayType, dynamic shapeHelper) {
return ContentShape("Fields_MyCustom",
field.Name,
f => f.Name(field.Name)
.SelectedValue(field.SelectedValue));
}
protected override DriverResult Editor(ContentPart part, MyCustomField field, dynamic shapeHelper) {
return ContentShape("Fields_MyCustom_Edit", () => shapeHelper.EditorTemplate(
TemplateName: TemplateName,
Model: field,
Prefix: GetPrefix(field, part)));
}
protected override DriverResult Editor(ContentPart part, MyCustomField field, IUpdateModel updater, dynamic shapeHelper) {
updater.TryUpdateModel(field, GetPrefix(field, part), null, null);
return Editor(part, field, shapeHelper);
}
}
MyModule/Views/Fields/MyCustom.cshtml:
@{
var selectedValue = Model.SelectedValue;
}
<h1>@selectedValue</h1>
MyModule/Views/EditorTemplates/Fields/MyCustom.cshtml:
@model MyModule.Fields.MyCustomField
<select id="@Html.IdFor(m => m.SelectedValue)" name="@Html.NameFor(m => m.SelectedValue)"></select>
@using (Script.Foot()) {
Script.Require("jQuery");
<script>
$(function () {
// your own url ofcourse
var url = 'http://jsonplaceholder.typicode.com/users',
dd = $("#@Html.IdFor(m => m.SelectedValue)");
$.getJSON(url, function (data) {
$.each(data, function () {
dd.append("<option value='" + this.name + "'>" + this.name + "</option>");
});
});
});
</script>
}
MyModule/Placement.信息:
<Placement>
<Place Fields_MyCustom_Edit="Content:3" />
<Place Fields_MyCustom="Content:3" />
</Placement>
这就是全部:)