2016-01-20 80 views
1

我使用OrchardCMS创建网站,在这里我希望在自定义窗体中有一个下拉菜单。 我已经创建了一个模块,我做了一个ajax调用,并返回下拉填充db提取值。 我只需要在自定义窗体中使用此下拉菜单。我想知道如何让它发生? 我已经尝试使用此链接创建自定义字段http://docs.orchardproject.net/Documentation/Creating-a-custom-field-type但我仍然找不到自己的位置。应该有一些方法来做到这一点。 请指导我如何去做。我感谢你的回应。 谢谢 Sohaib在Orchard CMS中创建自定义字段以渲染单独模块的下拉菜单

+0

你是什么意思与 '自定义表单'?创建该字段出了什么问题? – devqon

+0

在果园里,我们有一个模块名称作为自定义窗体,所以我启用了这个窗体,我们可以简单地将字段添加到该窗体。现在我需要的是以这种形式呈现我的下拉字段。所以,我希望你引导我。在这种情况下什么是最好的解决方案。 –

+0

我认为创建一个自定义字段将是要走的路,出了什么问题或什么不起作用? – devqon

回答

1

您可以简单地为此创建一个字段。

MyModule的/场/ MyCustomField.cs:

public class MyCustomField : ContentField { 
    public string SelectedValue { 
     get { return Storage.Get<string>(); } 
     set { Storage.Set(value); } 
    } 
} 

MyModule的/驱动器/ 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中/查看/场/ 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.info:

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

这应该是所有:)

+0

非常感谢所有这些,我对这个帮助非常满意。它清除了很多东西。你可以让我知道,在模型公共类MyCustomField属性SelectedValue 中的类中,当我在表单中插入此自定义字段后执行此代码时,它是空的。 然后抛出异常,说 第10行:@ Html.DropDownListFor(小部件=> widget.LayerId,新的SelectList(Model.AvailableLayers, “ID”, “姓名”)) 模块\ Orchard.Widgets \浏览\ EditorTemplates \ Parts.Widgets.WidgetPart.cshtml行:10 我需要知道这个变量第一次获取值的位置? –

+0

如果您发现它解决了您的原始问题,请接受此答案。如果您遇到更多问题,请创建一个新问题并提供更多详细信息:) – devqon

+0

非常感谢您亲爱的解决方案。它为我工作。你能告诉我有什么方法可以和果园家伙联系吗? –