Orchardcms 在Orchard CMS中创建自定义字段以呈现来自单独模块的下拉列表

Orchardcms 在Orchard CMS中创建自定义字段以呈现来自单独模块的下拉列表,orchardcms,Orchardcms,我创建的网站使用果园CMS,在这里我想有一个自定义形式的下拉列表。 我已经创建了一个模块,我对其进行了ajax调用,它返回填充了db获取值的下拉列表。 我所需要的就是在自定义表单中使用此下拉列表。我在想我怎样才能做到? 我曾尝试使用此链接创建自定义字段,但我仍然无法找到自己的位置。应该有办法做到这一点。 请指导我怎么做。我感谢你的答复。 谢谢 Sohaib您只需为此创建一个字段即可 MyModule/Fields/MyCustomField.cs: public class MyCustomFi

我创建的网站使用果园CMS,在这里我想有一个自定义形式的下拉列表。 我已经创建了一个模块,我对其进行了ajax调用,它返回填充了db获取值的下拉列表。 我所需要的就是在自定义表单中使用此下拉列表。我在想我怎样才能做到? 我曾尝试使用此链接创建自定义字段,但我仍然无法找到自己的位置。应该有办法做到这一点。 请指导我怎么做。我感谢你的答复。 谢谢
Sohaib

您只需为此创建一个字段即可

MyModule/Fields/MyCustomField.cs:

public class MyCustomField : ContentField {
    public string SelectedValue {
        get { return Storage.Get<string>(); }
        set { Storage.Set(value); }
    }
}
公共类MyCustomField:ContentField{
公共字符串SelectedValue{
get{return Storage.get();}
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);
    }
}
公共类MyCustomFieldDriver:ContentFieldDriver{
//编辑模板/Fields/MyCustom.cshtml
private const string TemplateName=“Fields/MyCustom”;
私有静态字符串GetPrefix(ContentField字段,ContentPart部分){
//处理字段名中的空格
返回(part.PartDefinition.Name+“+”字段名)
.替换(“,”);
}
受保护的重写驱动程序结果显示(ContentPart、MyCustomField字段、字符串显示类型、动态形状帮助){
返回ContentShape(“字段\我的自定义”,
字段名,
f=>f.Name(field.Name)
.SelectedValue(字段.SelectedValue));
}
受保护的重写驱动程序结果编辑器(ContentPart、MyCustomField字段、dynamic shapeHelper){
返回ContentShape(“字段\我的自定义\编辑”,()=>shapeHelper.EditorTemplate(
TemplateName:TemplateName,
型号:field,
前缀:GetPrefix(字段,部分));
}
受保护的重写驱动程序结果编辑器(ContentPart、MyCustomField字段、IUpdateModel更新程序、动态shapeHelper){
TryUpdateModel(字段,GetPrefix(字段,部分),null,null);
返回编辑器(零件、字段、形状帮助);
}
}
MyModule/Views/Fields/MyCustom.cshtml:

@{ 
    var selectedValue = Model.SelectedValue;
}

<h1>@selectedValue</h1>
@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>
}
@{
var selectedValue=Model.selectedValue;
}
@选择值
MyModule/Views/EditorTemplates/Fields/MyCustom.cshtml:

@{ 
    var selectedValue = Model.SelectedValue;
}

<h1>@selectedValue</h1>
@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>
}
@model MyModule.Fields.MyCustomField
@使用(Script.Foot()){
Script.Require(“jQuery”);
$(函数(){
//当然是你们自己的网址
var url='1〕http://jsonplaceholder.typicode.com/users',
dd=$(“#@Html.IdFor(m=>m.SelectedValue)”;
$.getJSON(url、函数(数据){
$。每个(数据、函数(){
dd.append(“+this.name+”);
});
});
});
}
MyModule/Placement.info:

<Placement>
  <Place Fields_MyCustom_Edit="Content:3" />
  <Place Fields_MyCustom="Content:3" />
</Placement>


就这些:)

你说的“定制”是什么意思?创建字段时出现了什么问题?在orchard中,我们有一个模块名作为自定义表单,因此我启用了该表单,我们可以简单地向该表单添加字段。现在我所需要的就是以这种形式呈现我的下拉字段。因此,我希望你能指导我。在这种情况下,什么是最好的解决方案。我认为创建一个自定义字段是最好的方法,什么出错了,什么不起作用了?我只是使用这个链接创建的,它是用于创建日期时间字段的。我想用板条箱装一个下降的场地,这样我就不知道该怎么做了。你知道我在哪里可以得到关于果园的帮助吗?任何你有想法的论坛或事物非常感谢你的所有这些我对你的帮助感到非常高兴。它清除了很多东西。当我在表单中插入此自定义字段后执行此代码时,请让我知道在模型内的类中公共类MyCustomField property SelectedValue为null。然后它抛出异常,说第10行:@Html.DropDownListFor(widget=>widget.LayerId,new SelectList(Model.AvailableLayers,“Id”,“Name”))在Modules\Orchard.Widgets\Views\EditorTemplates\Parts.Widgets.WidgetPart.cshtml行:10我需要知道这个变量第一次从哪里得到值?如果你发现它解决了你原来的问题,请接受这个答案。如果您遇到更多问题,请创建新问题并提供更多详细信息:)非常感谢您提供此解决方案,亲爱的。这对我有用。你能告诉我有什么方法可以和果园的伙计们联系吗?你能告诉我如果我想配置这个下拉列表,我应该怎么做吗。我需要在下拉列表中过滤数据,因此我想从管理员处对其进行配置,并希望在上面的ajax调用中发送该配置。。感谢查看此链接:用于字段范围设置,或此链接:如果您希望使用站点范围设置而不是每个字段