Ejemplo n.º 1
0
        public static MvcHtmlString SafeHiddenU(HtmlHelper htmlHelper, string field, Type type, int id = 0)
        {
            int index = TransformationContext.AddType(type, htmlHelper);

            if (index < 0)
            {
                object value = type.AssemblyQualifiedName;
                string sVal  = htmlHelper.Encode(Convert.ToString(value));
                field = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(field);

                return(MvcHtmlString.Create(
                           string.Format("<input type='hidden' id='{0}' name='{1}' value='{2}' />",
                                         id == 0 ? BasicHtmlHelper.IdFromName(field) : BasicHtmlHelper.IdFromName(field) + id.ToString(),
                                         field, sVal)));
            }
            else
            {
                string sindex = index.ToString(CultureInfo.InvariantCulture);
                field = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(field);
                return(MvcHtmlString.Create(
                           string.Format("<input type='hidden' id='{0}' name='{1}' value='{2}' />",
                                         id == 0 ? BasicHtmlHelper.IdFromName(field): BasicHtmlHelper.IdFromName(field) + id.ToString(),
                                         field, sindex)));
            }
        }
Ejemplo n.º 2
0
        public string InvokeNM(HtmlHelper fatherHelper, T model, string prefix)
        {
            ViewDataDictionary <T> dataDictionary = new ViewDataDictionary <T>(model);

            dataDictionary.TemplateInfo.HtmlFieldPrefix = prefix;
            BasicHtmlHelper.CopyRelevantErrors(dataDictionary.ModelState, fatherHelper.ViewData.ModelState, dataDictionary.TemplateInfo.HtmlFieldPrefix);

            return(InvokeNM(fatherHelper, dataDictionary));
        }
Ejemplo n.º 3
0
        }/*
          * public static MvcHtmlString SafeHiddenU(HtmlHelper htmlHelper, string field, Type type, int id = 0)
          * {
          * object value = type.AssemblyQualifiedName;
          * string sVal = htmlHelper.Encode(Convert.ToString(value));
          * field = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(field);
          *
          * return MvcHtmlString.Create(
          *         string.Format("<input type='hidden' id='{0}' name='{1}' value='{2}' />",
          *         id == 0 ? BasicHtmlHelper.IdFromName(field) : BasicHtmlHelper.IdFromName(field) + id.ToString(),
          *             field, sVal));
          * }*/

        public static MvcHtmlString SafeHiddenUC(HtmlHelper htmlHelper, string field, object value, string id = null)
        {
            string sVal = htmlHelper.Encode(Convert.ToString(value));

            field = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(field);

            return(MvcHtmlString.Create(
                       string.Format("<input type='hidden' id='{0}' name='{1}' value='{2}' />",
                                     id == null ? BasicHtmlHelper.IdFromName(field) : id,
                                     field, sVal)));
        }
Ejemplo n.º 4
0
        public string Invoke <M>(HtmlHelper <M> fatherHelper, T model, string prefix, string truePrefix = null)
        {
            ViewDataDictionary <T> dataDictionary = new ViewDataDictionary <T>(model);

            dataDictionary.TemplateInfo.HtmlFieldPrefix = prefix;
            if (truePrefix != null)
            {
                dataDictionary["_TruePrefix_"] = truePrefix;
            }
            BasicHtmlHelper.CopyRelevantErrors(dataDictionary.ModelState, fatherHelper.ViewData.ModelState, dataDictionary.TemplateInfo.HtmlFieldPrefix);

            return(Invoke(fatherHelper, dataDictionary));
        }
Ejemplo n.º 5
0
        public HtmlHelper BuildHelper(HtmlHelper fatherHelper, object model, string prefix, bool useContextWriter = false)
        {
            T tModel = default(T);

            if (model != null)
            {
                tModel = (T)model;
            }
            ViewDataDictionary <T> dataDictionary = new ViewDataDictionary <T>(tModel);

            dataDictionary.TemplateInfo.HtmlFieldPrefix = prefix;
            BasicHtmlHelper.CopyRelevantErrors(dataDictionary.ModelState, fatherHelper.ViewData.ModelState, dataDictionary.TemplateInfo.HtmlFieldPrefix);
            return(BuildHelper(fatherHelper, dataDictionary, useContextWriter));
        }
Ejemplo n.º 6
0
        static public void GetContainerTags(
            MVCControlsToolkit.Controls.ExternalContainerType externalContainerType,
            IDictionary <string, object> htmlattributes,
            out string openTag,
            out string closureTag)
        {
            switch (externalContainerType)
            {
            case MVCControlsToolkit.Controls.ExternalContainerType.div:
                openTag    = string.Format("<div {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</div>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.span:
                openTag    = string.Format("<span {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</span>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.table:
                openTag    = string.Format("<table {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</table>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.tr:
                openTag    = string.Format("<tr {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</tr>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.td:
                openTag    = string.Format("<td {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</td>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.th:
                openTag    = string.Format("<th {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</th>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.ul:
                openTag    = string.Format("<ul {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</ul>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.ol:
                openTag    = string.Format("<ol {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</ol>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.li:
                openTag    = string.Format("<li {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</li>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.section:
                openTag    = string.Format("<section {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</section>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.article:
                openTag    = string.Format("<article {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</article>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.tbody:
                openTag    = string.Format("<tbody {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</tbody>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.thead:
                openTag    = string.Format("<thead {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</thead>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.tfoot:
                openTag    = string.Format("<tfoot {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</tfoot>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.menu:
                openTag    = string.Format("<menu {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</menu>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.nav:
                openTag    = string.Format("<nav {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</nav>";
                break;

            case MVCControlsToolkit.Controls.ExternalContainerType.koComment:
                string attrs = htmlattributes["data-bind"] as string;
                if (attrs != null)
                {
                    attrs = attrs.Replace("&quot;", "'");
                }
                else
                {
                    attrs = string.Empty;
                }
                openTag    = string.Format("<!-- ko {0} -->", attrs);
                closureTag = "<!-- /ko -->";
                break;

            default:
                openTag    = string.Format("<p {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</p>";
                break;
            }
            return;
        }
Ejemplo n.º 7
0
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            string displayPrefix = bindingContext.ModelName;

            if (displayPrefix == "model" || (bindingContext.ModelMetadata.PropertyName == null && !displayPrefix.Contains('.') && !displayPrefix.Contains('[') && !displayPrefix.Contains('$')))
            {
                displayPrefix = "display";
            }
            ValueProviderResult attemptedTransformer = bindingContext.ValueProvider.GetValue(displayPrefix + ".$$");

            if (displayPrefix == "display" && attemptedTransformer == null)
            {
                attemptedTransformer = bindingContext.ValueProvider.GetValue("$$");
            }
            if (attemptedTransformer != null && attemptedTransformer.AttemptedValue != null)
            {
                Type displayModelType = BasicHtmlHelper.DecodeDisplayInfo(attemptedTransformer.AttemptedValue, bindingContext.ValueProvider);

                if (displayModelType != null && displayModelType.IsClass)
                {
                    object[] displayModelContext = null;

                    if (displayModelType.GetInterface("IDisplayModel") != null)
                    {
                        displayModelContext = new object[] { bindingContext.ModelName, controllerContext.HttpContext.Items };
                    }
                    else if (displayModelType.GetInterface("IDisplayModelBuilder") != null)
                    {
                        object displayModelBuilder = displayModelType.GetConstructor(new Type[0]).Invoke(new object[0]);
                        IDisplayModelBuilder displayModelFarmInterface = displayModelBuilder as IDisplayModelBuilder;
                        if (displayModelFarmInterface != null)
                        {
                            displayModelContext = displayModelFarmInterface.DisplayModelContext;
                            displayModelType    = displayModelFarmInterface.DisplayModelType;
                            if (displayModelType.GetInterface("IDisplayModel") == null)
                            {
                                displayModelType = null;
                            }
                        }
                    }
                    else
                    {
                        displayModelType = null;
                    }
                    if (displayModelType != null)
                    {
                        IDisplayModel displayModel = BindDisplayModel(controllerContext, bindingContext, displayModelType) as IDisplayModel;
                        if (displayModel != null)
                        {
                            try
                            {
                                IUpdateModelState msUpdater = displayModel as IUpdateModelState;
                                if (msUpdater != null)
                                {
                                    msUpdater.GetCurrState(displayPrefix, -1, bindingContext.ModelState);
                                }
                                return(UpdateModel(controllerContext, bindingContext,
                                                   displayModel.ExportToModel(bindingContext.ModelType, displayModelContext)));
                            }
                            catch (Exception ex)
                            {
                                bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format(ex.Message, bindingContext.ModelMetadata.GetDisplayName()));
                                return(null);
                            }
                        }
                    }
                }
            }
            object res = null;

            try
            {
                res = base.BindModel(controllerContext, bindingContext);
            }
            catch { }
            return
                (UpdateModel(controllerContext, bindingContext,
                             res));
        }
Ejemplo n.º 8
0
        protected object UpdateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, object model)
        {
            bool       cont         = true;
            int        index        = 0;
            int        notNullCount = 0;
            int        prevErrors;
            List <int> trueIndexes       = null;
            Type       previousModelType = null;

            while (cont)
            {
                string displayPrefix = bindingContext.ModelName;
                if (displayPrefix == "model" || (bindingContext.ModelMetadata.PropertyName == null && !displayPrefix.Contains('.') && !displayPrefix.Contains('[') && !displayPrefix.Contains('$')))
                {
                    displayPrefix = "updatemodel";
                }
                ValueProviderResult attemptedTransformer = bindingContext.ValueProvider.GetValue(displayPrefix + string.Format(".$${0}", index));
                if (attemptedTransformer != null && attemptedTransformer.AttemptedValue != null)
                {
                    Type displayModelType = BasicHtmlHelper.DecodeUpdateInfo(attemptedTransformer.AttemptedValue, previousModelType, bindingContext.ValueProvider);

                    if (displayModelType != null && displayModelType.IsClass && displayModelType.GetInterface("IUpdateModel") != null)
                    {
                        prevErrors        = bindingContext.ModelState.Keys.Count;
                        previousModelType = displayModelType;
                        IUpdateModel displayModel = BindDisplayModel(controllerContext, bindingContext, displayModelType, string.Format(".$${0}.$", index)) as IUpdateModel;
                        if (trueIndexes == null)
                        {
                            trueIndexes = new List <int>();
                        }
                        if (displayModel == null)
                        {
                            trueIndexes.Add(-1);
                        }
                        else
                        {
                            string[]            fields = null;
                            ValueProviderResult fieldsAttemptedTransformer = bindingContext.ValueProvider.GetValue(displayPrefix + string.Format(".$${0}.f$ields", index));
                            if (fieldsAttemptedTransformer != null && !string.IsNullOrWhiteSpace(fieldsAttemptedTransformer.AttemptedValue))
                            {
                                fields = BasicHtmlHelper.DecodeFieldsInfo(fieldsAttemptedTransformer.AttemptedValue).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            else
                            {
                                fields = new string[0];
                            }
                            int         beforeElements   = -1;
                            ICollection beforeCollection = model as ICollection;

                            if (beforeCollection != null)
                            {
                                beforeElements = beforeCollection.Count;
                            }
                            IUpdateModelState updateModelState = displayModel as IUpdateModelState;
                            bool finished = false;
                            if (updateModelState != null)
                            {
                                if (updateModelState.MoveState && index != notNullCount)
                                {
                                    trueIndexes.Add(notNullCount);
                                    AlignModelState(
                                        bindingContext.ModelState,
                                        trueIndexes,
                                        LowerModelName(displayPrefix, ".$$"));
                                    finished = true;
                                }

                                updateModelState.GetCurrState(displayPrefix, notNullCount, bindingContext.ModelState);
                            }
                            IVisualState visualStateStorage = displayModel as IVisualState;
                            if (visualStateStorage != null)
                            {
                                visualStateStorage.ModelName = bindingContext.ModelName;
                                visualStateStorage.Store     = controllerContext.HttpContext.Items;
                            }
                            IHandleUpdateIndex handleUpdateIndex = displayModel as IHandleUpdateIndex;
                            if (handleUpdateIndex != null)
                            {
                                handleUpdateIndex.Index      = index;
                                handleUpdateIndex.ModelState = bindingContext.ModelState;
                            }
                            try
                            {
                                model = displayModel.UpdateModel(model, fields);
                            }
                            catch (Exception ex)
                            {
                                bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format(ex.Message, bindingContext.ModelMetadata.GetDisplayName()));
                            }
                            if (finished)
                            {
                                break;
                            }
                            int         afterElements   = -1;
                            ICollection afterCollection = model as ICollection;
                            if (afterCollection != null)
                            {
                                afterElements = afterCollection.Count;
                            }
                            bool noCollections = afterElements == -1 && beforeElements == -1;

                            if (model != null && (noCollections || beforeElements != afterElements))
                            {
                                trueIndexes.Add(notNullCount);

                                notNullCount++;
                            }
                            else
                            {
                                trueIndexes.Add(-1);
                            }
                        }
                    }
                }
                else
                {
                    if (index != notNullCount)
                    {
                        AlignModelState(
                            bindingContext.ModelState,
                            trueIndexes,
                            LowerModelName(displayPrefix, ".$$"));
                    }
                    cont = false;
                    break;
                }
                index++;
            }
            return(model);
        }