Beispiel #1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            (_htmlHelper as IViewContextAware).Contextualize(ViewContext);
            var prefix = ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix;

            var htmlId = prefix.IsEmpty()
                ? ModelItem.Metadata.PropertyName
                : _htmlHelper.IdForModel() != ModelItem.Metadata.PropertyName
                    ? _htmlHelper.IdForModel() + "_" + ModelItem.Metadata.PropertyName
                    : ModelItem.Metadata.PropertyName;

            var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName;

            var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last();

            if (ItemList == null)
            {
                var comboDataInfo = GetComboBoxDataSource(ModelItem.ModelExplorer);
                ItemList = comboDataInfo.ListItems;
                if (comboDataInfo.CanSelect)
                {
                    if (OptionLabel.IsEmpty())
                    {
                        OptionLabel = UI.Select;
                    }
                }
                else
                {
                    OptionLabel = null;
                }
            }
            var sbOptions = new StringBuilder();

            if (!OptionLabel.IsEmpty())
            {
                sbOptions.AppendLine(string.Concat("<option value=\"\">", OptionLabel, "</option>"));
            }
            if (ItemList != null)
            {
                foreach (var item in ItemList)
                {
                    var option = new TagBuilder("option");
                    option.Attributes.Add("value", item.Value);
                    if (item.Selected)
                    {
                        option.Attributes.Add("selected", "selected");
                    }
                    option.InnerHtml.AppendHtml(item.Text);
                    sbOptions.AppendLine(option.GetString());
                }
            }
            else if (ItemList2 != null)
            {
                foreach (var item in ItemList2)
                {
                    var option = new TagBuilder("option");
                    option.Attributes.Add("value", item.Value);
                    if (item.Selected)
                    {
                        option.Attributes.Add("selected", "selected");
                    }
                    option.InnerHtml.AppendHtml(item.Text);
                    sbOptions.AppendLine(option.GetString());
                }
            }

            if (DisableEditorTemplate)
            {
                output.TagName = "select";
                output.TagMode = TagMode.StartTagAndEndTag;
                if (!DataUrl.IsEmpty())
                {
                    output.Attributes.Add("data-url", DataUrl);
                }
                output.Attributes.Add("name", htmlName);
                output.Attributes.Add("id", htmlId);
                if (ModelItem.Metadata.IsRequired)
                {
                    var requiredAttr = Digger2.GetMetaAttr <RequiredAttribute>(ModelItem.Metadata);
                    var errMsg       = "";
                    if (requiredAttr != null)
                    {
                        errMsg = requiredAttr.ErrorMessage;
                        if (errMsg.IsEmpty() && requiredAttr.ErrorMessageResourceType != null)
                        {
                            var rm  = new ResourceManager(requiredAttr.ErrorMessageResourceType);
                            var rsm = rm.GetString(requiredAttr.ErrorMessageResourceName);
                            if (rsm != null && !rsm.IsEmpty())
                            {
                                errMsg = string.Format(rsm, labelText);
                            }
                        }
                    }
                    output.Attributes.Add("data-val", "true");
                    output.Attributes.Add("data-val-required",
                                          requiredAttr != null
                            ? errMsg
                            : "Gereklidir"
                                          );
                }
                output.Content.SetHtmlContent(sbOptions.ToString());
            }
            else
            {
                output.TagName = "div";
                output.TagMode = TagMode.StartTagAndEndTag;
                output.Attributes.Add("class", "form-group");
                output.Attributes.Add("id", "div" + htmlId);

                var select = new TagBuilder("select");
                if (!DataUrl.IsEmpty())
                {
                    select.Attributes.Add("data-url", DataUrl);
                }
                select.AddCssClass("form-control input-sm");
                if (!CssClass.IsEmpty())
                {
                    select.AddCssClass(CssClass);
                }
                select.Attributes.Add("name", htmlName);
                select.Attributes.Add("id", htmlId);

                var irq          = (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) || (IsRequired.HasValue && IsRequired.Value);
                var requiredHtml = irq
                    ? $"<span class=\"{RequiredCssClass}\">{Symbol}</span>"
                    : "";
                var metaHtml = irq
                    ? $"<span class=\"field-validation-valid help-block\" data-valmsg-for=\"{htmlId}\" data-valmsg-replace=\"true\"></span>"
                    : "";

                if (irq)
                {
                    var requiredAttr = Digger2.GetMetaAttr <RequiredAttribute>(ModelItem.Metadata);
                    var errMsg       = "";
                    if (requiredAttr != null)
                    {
                        errMsg = requiredAttr.ErrorMessage;
                        if (errMsg.IsEmpty() && requiredAttr.ErrorMessageResourceType != null)
                        {
                            var rm  = new ResourceManager(requiredAttr.ErrorMessageResourceType);
                            var rsm = rm.GetString(requiredAttr.ErrorMessageResourceName);
                            if (rsm != null && !rsm.IsEmpty())
                            {
                                errMsg = string.Format(rsm, labelText);
                            }
                        }
                    }
                    select.Attributes.Add("data-val", "true");
                    select.Attributes.Add("data-val-required",
                                          requiredAttr != null
                            ? errMsg
                            : "Gereklidir"
                                          );
                }
                select.InnerHtml.AppendHtml(sbOptions.ToString());

                var addHtml = "";
                if (!AddNewUrl.IsEmpty())
                {
                    addHtml = $@"<div class='col-xs-1'>
                <a href='{AddNewUrl}' class='btn btn-xs btn-primary fancyboxAdd fancybox.iframe'><i class='fa fa-plus'></i></a>
            </div>";
                    EditorColCount--;
                }

                var editorTemplate = $@"
                    <label class=""col-xs-{LabelColCount} control-label"">{labelText} {requiredHtml}</label>
                    <div class=""col-xs-{EditorColCount}"">
                        {select.GetString()}
                        {metaHtml}
                    </div>
                    {addHtml}";
                output.Content.SetHtmlContent(editorTemplate);
            }
        }