示例#1
0
        private static MvcHtmlString SerializeInternal(
            HtmlHelper htmlHelper,
            string name,
            object data,
            bool useViewData,
            MvcSerializer serializer
            )
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException("htmlHelper");
            }

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
            }

            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            if (useViewData)
            {
                data = htmlHelper.ViewData.Eval(name);
            }

            string serializedData = (serializer ?? new MvcSerializer()).Serialize(data);

            TagBuilder builder = new TagBuilder("input");

            builder.Attributes["type"]  = "hidden";
            builder.Attributes["name"]  = name;
            builder.Attributes["value"] = serializedData;
            return(MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing)));
        }
        private static MvcHtmlString SerializeInternal(HtmlHelper htmlHelper, string name, object data, SerializationMode mode, bool useViewData, MvcSerializer serializer)
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException("htmlHelper");
            }

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
            }

            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            if (useViewData)
            {
                data = htmlHelper.ViewData.Eval(name);
            }

            string serializedData = (serializer ?? new MvcSerializer()).Serialize(data, mode);

            TagBuilder builder = new TagBuilder("input");
            builder.Attributes["type"] = "hidden";
            builder.Attributes["name"] = name;
            builder.Attributes["value"] = serializedData;
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }
示例#3
0
        public void SerializeAllowsNullValues() {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act
            string serializedValue = serializer.Serialize(null, SerializationMode.Plaintext);

            // Assert
            Assert.AreEqual(@"/wFk", serializedValue);
        }
        public void DeserializeThrowsIfSerializedValueIsNull() {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                    serializer.Deserialize(null, SerializationMode.Signed);
                }, "serializedValue");
        }
示例#5
0
        public void SerializeAndDeserializeRoundTripsValue() {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act
            string serializedValue = serializer.Serialize(42, SerializationMode.Plaintext);
            object deserializedValue = serializer.Deserialize(serializedValue, SerializationMode.Plaintext);

            // Assert
            Assert.AreEqual(42, deserializedValue, "Value was not round-tripped properly.");
        }
示例#6
0
        public void DeserializeThrowsIfSerializedValueIsCorrupt() {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            Exception exception = ExceptionHelper.ExpectException<SerializationException>(
                delegate {
                    serializer.Deserialize("This is a corrupted value.", SerializationMode.Plaintext);
                },
                @"Deserialization failed. Verify that the data is being deserialized using the same SerializationMode with which it was serialized. Otherwise see the inner exception.");

            Assert.IsNotNull(exception.InnerException, "Inner exception was not propagated correctly.");
        }
        public void DeserializeThrowsIfModeIsOutOfRange() {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            ExceptionHelper.ExpectArgumentOutOfRangeException(
                delegate {
                    serializer.Serialize("blah", (SerializationMode)(-1));
                },
                "mode",
                @"The provided SerializationMode is invalid.
Parameter name: mode");
        }
示例#8
0
 internal static MvcHtmlString Serialize(
     this HtmlHelper htmlHelper,
     string name,
     MvcSerializer serializer
     )
 {
     return(SerializeInternal(
                htmlHelper,
                name,
                null,
                useViewData: true,
                serializer: serializer
                ));
 }
            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
                if (bindingContext == null) {
                    throw new ArgumentNullException("bindingContext");
                }

                ValueProviderResult vpResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                if (vpResult == null) {
                    // nothing found
                    return null;
                }

                MvcSerializer serializer = new MvcSerializer();
                string serializedValue = (string)vpResult.ConvertTo(typeof(string));
                return serializer.Deserialize(serializedValue, _mode);
            }
示例#10
0
 internal static MvcHtmlString Serialize(
     this HtmlHelper htmlHelper,
     string name,
     object data,
     MvcSerializer serializer
     )
 {
     return(SerializeInternal(
                htmlHelper,
                name,
                data,
                useViewData: false,
                serializer: serializer
                ));
 }
示例#11
0
            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                if (bindingContext == null)
                {
                    throw new ArgumentNullException("bindingContext");
                }

                ValueProviderResult vpResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

                if (vpResult == null)
                {
                    // nothing found
                    return(null);
                }

                MvcSerializer serializer      = new MvcSerializer();
                string        serializedValue = (string)vpResult.ConvertTo(typeof(string));

                return(serializer.Deserialize(serializedValue, _mode));
            }
 public DeserializingModelBinder(SerializationMode mode, MvcSerializer serializer)
 {
     _mode = mode;
     _serializer = serializer ?? new MvcSerializer();
 }
 internal static MvcHtmlString Serialize(this HtmlHelper htmlHelper, string name, MvcSerializer serializer)
 {
     return SerializeInternal(htmlHelper, name, null, useViewData: true, serializer: serializer);
 }
示例#14
0
 public DeserializingModelBinder(MvcSerializer serializer)
 {
     _serializer = serializer ?? new MvcSerializer();
 }
 internal static MvcHtmlString Serialize(this HtmlHelper htmlHelper, string name, object data, MvcSerializer serializer)
 {
     return SerializeInternal(htmlHelper, name, data, useViewData: false, serializer: serializer);
 }
示例#16
0
 public DeserializingModelBinder(SerializationMode mode, MvcSerializer serializer)
 {
     _mode       = mode;
     _serializer = serializer ?? new MvcSerializer();
 }
 internal static MvcHtmlString Serialize(this HtmlHelper htmlHelper, string name, object data, SerializationMode mode, MvcSerializer serializer) {
     return SerializeInternal(htmlHelper, name, data, mode, false /* useViewData */, serializer);
 }
 internal static MvcHtmlString Serialize(this HtmlHelper htmlHelper, string name, SerializationMode mode, MvcSerializer serializer) {
     return SerializeInternal(htmlHelper, name, null, mode, true /* useViewData */, serializer);
 }
 internal static MvcHtmlString Serialize(this HtmlHelper htmlHelper, string name, object data, SerializationMode mode, MvcSerializer serializer)
 {
     return(SerializeInternal(htmlHelper, name, data, mode, false /* useViewData */, serializer));
 }
 public DeserializingModelBinder(MvcSerializer serializer)
 {
     _serializer = serializer ?? new MvcSerializer();
 }
 internal static MvcHtmlString Serialize(this HtmlHelper htmlHelper, string name, SerializationMode mode, MvcSerializer serializer)
 {
     return(SerializeInternal(htmlHelper, name, null, mode, true /* useViewData */, serializer));
 }