Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/11/2014, 13:26
warbandit69
 
Fecha de Ingreso: diciembre-2008
Ubicación: http://www.solucionesrios.tk/
Mensajes: 413
Antigüedad: 15 años, 4 meses
Puntos: 19
Busqueda Problemas al deserializar un json en un modelo de datos

Buenas tardes tengo este problema:

Código:
Se detectó System.InvalidOperationException
  HResult=-2146233079
  Message=No se puede convertir el objeto de tipo 'System.String' en el tipo 'kambi4.Areas.INVENTORY.Models.SheetModel'
  Source=System.Web.Extensions
  StackTrace:
       en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
       en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
       en System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
       en System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
       en kambi4.Areas.INVENTORY.Controllers.PROG34Controller.SaveSheet(String collection, Int32 contador) en D:\PC\Visual Studio 2010\Projects\kambi4\kambi4\Areas\INVENTORY\Controllers\PROG34Controller.cs:línea 488
  InnerException:
El cual es causado al tratar de deserializar una linea json enviada desde un ajax jquery, seria la siguiente:

Código:
"\"ppms=123&total_hours=123&failed_not=on&tool_hours=123&serial1=123&serial_in=123&serial_out=123&mud_type=123&selenoid_in=123&selenoid_out=123&operatorX=123&client=1003&job_number=123&comments=123&FlowSwithSensitivityIN=123&FlowSwithSensitivityOUT=123&FlowSwitchVoltIN=123&FlowSwitchVoltOUT=123&NomninalValue1IN=v&NomninalValue1OUT=123&AverageCurrentIN=123&AverageCurrentOUT=123&NomninalValue2IN=123&NomninalValue2OUT=123&PullingCurrentIN=123&PullingCurrentOUT=123&NomninalValue3IN=123&NomninalValue3OUT=123&HoldingCurrentIN=123&HoldingCurrentOUT=123&NomninalValue4IN=123&NomninalValue4OUT=123&PullStrengthIN=123&PullStrengthOUT=123&NomninalValue5IN=123&NomninalValue5OUT=123&HoldStrengthIN=123&HoldStrengthOUT=v&NomninalValue6IN=123&NomninalValue6OUT=123&PoppetGapIN=123&PoppetGapOUT=123&NomninalValue7IN=123&NomninalValue7OUT=123&MembranePressureIN=123&MembranePressureOUT=123&NomninalValue8IN=123&NomninalValue8OUT=123&MembraneConditionIN=123&MembraneConditionOUT=123&NomninalValue9IN=123&NomninalValue9OUT=123&BellowConditionIN=123&BellowConditionOUT=123&NomninalValue10IN=123&NomninalValue10OUT=123&SnubberShockIN=123&SnubberShockOUT=123&NomninalValue11IN=123&NomninalValue11OUT=123&MDMConditionIN=123&MDMConditionOUT=123&NomninalValue12IN=123&NomninalValue12OUT=123&ServoPoppetIN=123&ServoPoppetOUT=123&ServoOrificeIN=123&ServoOrificeOUT=123&OilConditionIN=123&OilConditionOUT=123&NomninalValue13IN=123&NomninalValue13OUT=123&KintecConditionIN=123&KintecConditionOUT=123&NomninalValue14IN=123&NomninalValue14OUT=123&ORingsConditionIN=123&ORingsConditionOUT=123&NomninalValue15IN=123&NomninalValue15OUT=123&HousingConditionIN=123&HousingConditionOUT=123&NomninalValue16IN=123&InspectionNotes=123&technicians=1003&InDate=11%2F12%2F2014&OutDate=11%2F13%2F2014&SheetNumber=&idserial1=2005\""
Este es el script jquery en la vista:

Código:
function GuardarHoja() {
        //debugger;
        var formCollection = JSON.stringify($('#form_sheet').serialize()); // .serialize() make query string with form inputs name and value
        $.ajax({
            url: '/INVENTORY/PROG34/SaveSheet', // script url to send
            type: 'POST', // method of sending
            data: { collection: formCollection, contador: contador },
            success: function (data) {
                mostrar_alert_ui(data.titulo, data.mensaje, 350);
                if (data.success) {
                    guardar_items();
                    var div = $("#MaintenanceSheet");
                    div.dialog('close');
                    RestablecerTodo();
                }
            },
            error: function (xhRequest, ErrorText, thrownError) {
                alert("Failed to Save Sheet, please try again");
                console.log('xhRequest: ' + xhRequest + "\n");
                console.log('ErrorText: ' + ErrorText + "\n");
                console.log('thrownError: ' + thrownError + "\n");
            }
        });
    }
Este es en el controlador (donde me arroja el error):

Código:
public JsonResult SaveSheet(string collection, int contador)
        {
            object jsonData = null;
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var b = serializer.Deserialize<SheetModel>(collection);

                /*
                Mucha lógica aqui
                */

                jsonData = new
                {
                    success = true,
                    mensaje = "Maintenance Sheet had been saved",
                    titulo = "New Sheet Saved"
                };

            }
            catch (Exception ex)
            {
                jsonData = new
                {
                    success = false,
                    mensaje = ex.ToString(),
                    titulo = "ERROR"
                };
            }
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GuardarItemsUsados(string qty,string part,string serial,string description)
        {
            object jsonData = null; 
            try
            {
                int ultimoId = (from d in dc.sheets orderby d.idsheet descending select d.idsheet).FirstOrDefault();

                var a = new INVENTORY.Models.useditems();

                a.description = description;
                a.idsheet = ultimoId;
                a.part = part;
                a.qty = Convert.ToInt32(qty);
                a.serial = serial;

                dc.useditems.InsertOnSubmit(a);
                dc.SubmitChanges();

                jsonData = new
                {
                    success = true
                };
            }
            catch (Exception ex)
            {
                jsonData = new {
                    mensaje = ex.ToString(),
                    titulo = "ERROR",
                    success = false
                };
            }
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

Y este es el modelo:

Código:
public class SheetModel
    {
        public string ppms { get; set; }
        public string total_hours { get; set; }
        public int idserial1 { get; set; }
        public bool failed_yes { get; set; }
        public bool failed_not { get; set; }
        public string tool_hours { get; set; }
        public string ServoPoppetIN { get; set; }
        public string ServoPoppetOUT { get; set; }
        public string ServoOrificeIN { get; set; }
        public string ServoOrificeOUT { get; set; }
        public string OilConditionIN { get; set; }
        public string OilConditionOUT { get; set; }
        public string KintecConditionIN { get; set; }
        public string KintecConditionOUT { get; set; }
        public string ORingsConditionIN { get; set; }
        public string ORingsConditionOUT { get; set; }
        public string HousingConditionIN { get; set; }
        public string HousingConditionOUT { get; set; }
        public string NomninalValue16IN { get; set; }
        public string InspectionNotes { get; set; }
        public int technicians { get; set; }
        public DateTime InDate { get; set; }
        public DateTime OutDate { get; set; }
        public string SheetNumber { get; set; }
        public string NomninalValue1IN { get; set; }
        public string NomninalValue2IN { get; set; }
        public string NomninalValue3IN { get; set; }
        public string NomninalValue4IN { get; set; }
        public string NomninalValue5IN { get; set; }
        public string NomninalValue6IN { get; set; }
        public string NomninalValue7IN { get; set; }
        public string NomninalValue8IN { get; set; }
        public string NomninalValue9IN { get; set; }
        public string NomninalValue10IN { get; set; }
        public string NomninalValue11IN { get; set; }
        public string NomninalValue12IN { get; set; }
        public string NomninalValue13IN { get; set; }
        public string NomninalValue14IN { get; set; }
        public string NomninalValue15IN { get; set; }
        public string NomninalValue1OUT { get; set; }
        public string NomninalValue2OUT { get; set; }
        public string NomninalValue3OUT { get; set; }
        public string NomninalValue4OUT { get; set; }
        public string NomninalValue5OUT { get; set; }
        public string NomninalValue6OUT { get; set; }
        public string NomninalValue7OUT { get; set; }
        public string NomninalValue8OUT { get; set; }
        public string NomninalValue9OUT { get; set; }
        public string NomninalValue10OUT { get; set; }
        public string NomninalValue11OUT { get; set; }
        public string NomninalValue12OUT { get; set; }
        public string NomninalValue13OUT { get; set; }
        public string NomninalValue14OUT { get; set; }
        public string NomninalValue15OUT { get; set; }
    }
__________________
http://www.solucionesrios.tk/

Visita mi Web!

Última edición por warbandit69; 11/11/2014 a las 13:28 Razón: Codigo