Selecting values from an object

Example:
When entering an address in the CRM module, selecting a city from the list of the City object instances must be possible
 
  1. Create a City object in the Designer (or use an existing one, if any).
  2. Copy the view file from D:\Work3\EleWise.ELMA\Applications\BPM\EleWise.ELMA.BPM.Web\Modules\EleWise.ELMA.CRM.Web\Views\Shared\EditorTemplates\Entities\Address.cshtml to the configuration folder \WebApplication\Modules\EleWise.ELMA.CRM.Web\Views\Shared\EditorTemplates\Entities\Address.cshtml.
  3. Change the code in this file:
 
At the beginning of the file
@using EleWise.ELMA.Web.Mvc.Models.Boxes
@{
   // Object UID
   var cityCatalogUid = "00da4d30-3091-4f5f-b6ce-9747a6113827";
}
 
 
Replace the old field for specifying a city (@Html.EditableProperty(m => m.City)) with the following code
@{
    // Set the current value
    var selectedItems = Model != null && !string.IsNullOrEmpty( Model.City )
      ? new List <AutocompleteItem> { new AutocompleteItem { Text = Model.City.ToString(), Value = "" } }
      : new List<AutocompleteItem>();
}
 
 
@Html.AutocompleteComboBox(new AutocompleteComboBox
{
   // Allow manual input
   AutoFill = false,
   HtmlAttributes = new HtmlAttributes { style = new HtmlStyle { width = "300px" } },
 
   // Method and controller that returns object data for Select
   ActionName = "Select",
   ControllerName = "Entity",
   RouteValueDictionary = new RouteValueDictionary { { "area" , "EleWise.ELMA.BPM.Web.Common" }, { "uid", new Guid(cityCatalogUid) } },
 
   // Element name on the page
   Name = ViewData.TemplateInfo .GetFullHtmlFieldName( "CityCatalogCombobox"),
   SelectedEntities = selectedItems,
 
   // What happens on the form upon changing the value
   OnChange = string.Format( "function (e) {{ OnAddressCitySelect(e, ’{0}’); }}", ViewData.TemplateInfo.GetFullHtmlFieldName("City"))
})
@Html.HiddenFor(m => m.City)
 
@Html.AutocompleteComboBox(new AutocompleteComboBox { // Allow manual input AutoFill = false, HtmlAttributes = new HtmlAttributes { style = new HtmlStyle { width = "300px" } }, // Method and controller, which returns the object data for Select ActionName = "Select", ControllerName = "Entity", RouteValueDictionary = new RouteValueDictionary { { "area" , "EleWise.ELMA.BPM.Web.Common" }, { "uid", new Guid(cityCatalogUid) } }, // Element name on the page Name = ViewData.TemplateInfo .GetFullHtmlFieldName( "CityCatalogCombobox"), SelectedEntities = selectedItems, // What happens on the form upon changing the value OnChange = string.Format( "function (e) {{ OnAddressCitySelect(e, ’{0}’); }}", ViewData.TemplateInfo.GetFullHtmlFieldName("City")) }) @Html.HiddenFor(m => m.City)
 
Note
For the code to work correctly, replace ’{0}’ with ’{0}’ in line 17.
The script that will be writing the selected value to the hidden field of the model:
 
@{
   Html.RegisterContent("OnAddressCitySelect",@<text>
      <script type="text/javascript">
         function OnAddressCitySelect (e, cityId) {
            $(’#’ + cityId.replace(/\./g, ’_’))[0].value = $(’#’ + e.target.id).data(’tComboBox’).text();
         }
      </script>
   </text>);
}