vista ventajas tutorial net mvc modelo form estructura español curso controlador asp asp.net-mvc json knockout.js knockout-mapping-plugin

asp.net mvc - ventajas - ¿Por qué mi presentación de Knockout no se une a mi modelo de MVC?



razor mvc (3)

Sí, necesita especificar el tipo de datos así como el contentType como por ejemplo:

$.ajax({    type: "POST",    url: "/person/create",    dataType: "json",    contentType: "application/json; charset=utf-8",    data: jsonData,    success: function (result){ ...    },    error: function (error){ ...    } });

Eso debería funcionar bien (lo he resaltado aquí si estás interesado).

Tengo un modelo knockout que está alimentando mi página y en un guardar el siguiente código se ejecuta en el lado del cliente.

self.save = function() { var tirerun = ko.mapping.toJSON(self.data()); $.ajax({ type: ''post'', url: "@Url.Action("SaveTireRunModel", "Events")", data: tirerun, success: function(result) { if (!result.success) { alert(result.error); } else {} } }); };

A continuación, publica el siguiente JSON en mi servidor (desde el violín y recortado para abreviar)

Con el siguiente encabezado de solicitud http

POST http://localhost:63648/Events/SaveTireRunModel HTTP/1.1 Host: localhost:63648 Connection: keep-alive Content-Length: 1975 Origin: http://localhost:63648 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Accept: */* Referer: http://localhost:63648/Events/OutingDetails?outingID=010fd8a1-e3a5-e111-a660-f0def1589f4b Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: jstree_open=%232012%2C%23c914324c-4d9f-e111-a79b-0024e8a037d3%2C%23eb-e957-e111-9e70-d067e53b2ed6%2C%234d-c00e-e111-ad1c-0e6e17349d3e%2C%2357-c00e-e111-ad1c-0e6e17349d3e%2C%234d-c00e-e111-ad1c-0e6e17349d3e; jstree_load=; .ASPXAUTH=054DD21DC0A17DDDAAFC254500ED8D6B3299EAABF1FB647732618E956EF082880BBA0B52A59849D3362AA03EB1F27D28867B07CD8451E4E983D84365608AA145F8DF8D71C2481639E110EC31325EE27EA3281FA2EDB7AD31A744D2A9918BE6599DB97E489ED4598D2DF63C31E36E809A282CF96FFCE5ACAEF8308FEA3DF1C6DE50B2AA7299F9D9AF0C30CE867DCCA352; jstree_open=%232012%2C%233dcc4b9b-37a3-e111-a6d3-0026b99ca1da%2C%23eb-e957-e111-9e70-d067e53b2ed6%2C%2357-c00e-e111-ad1c-0e6e17349d3e%2C%23a2962e1b-45a5-e111-b9ac-f0def1589f4b; jstree_load=%2311a4611a-8851-e111-95c7-60d819fcced5%2C%23b89422a0-0d03-e011-99cb-d601d71f0fcf; jstree_select=%23010fd8a1-e3a5-e111-a660-f0def1589f4b {"Comments":"RSTA Dwn 2 Holes (6)/r/nRF Spring to 35468/r/nCross Dwn to 47.9 (-0.2)","TireSetsList":[{"ID":"1","Value":"1"},{"ID":"2","Value":"2"},{"ID":"3","Value":"3"},{"ID":"4","Value":"4"},{"ID":"5","Value":"5"}],"LFShoulderTemp":245,"RFShoulderTemp":245,"LFOuterTemp":177,"LFMiddleTemp":null,"LFInnerTemp":200,"RFOuterTemp":177,"RFMiddleTemp":null,"RFInnerTemp":200,"LROuterTemp":177,"LRMiddleTemp":null,"LRInnerTemp":200,"RROuterTemp":177,"RRMiddleTemp":null,"RRInnerTemp":200,"LFInitialOuterWear":null,"LFInitialOuterMiddleWear":null,"LFInitialMiddleWear":null,"LFInitialInnerMiddleWear":null,"LFInitialInnerWear":null,"LFAfterOuterWear":null,"LFAfterOuterMiddleWear":null,"LFAfterMiddleWear":200,"LFAfterInnerMiddleWear":null,"LFAfterInnerWear":null,"RFInitialOuterWear":null,"RFInitialOuterMiddleWear":null,"RFInitialMiddleWear":null,"RFInitialInnerMiddleWear":null,"RFInitialInnerWear":null,"RFAfterOuterWear":null,"RFAfterOuterMiddleWear":null,"RFAfterMiddleWear":200,"RFAfterInnerMiddleWear":null,"RFAfterInnerWear":null,"LRInitialOuterWear":null,"LRInitialOuterMiddleWear":null,"LRInitialMiddleWear":null,"LRInitialInnerMiddleWear":null,"LRInitialInnerWear":null,"LRAfterOuterWear":null,"LRAfterOuterMiddleWear":null,"LRAfterMiddleWear":200,"LRAfterInnerMiddleWear":null,"LRAfterInnerWear":null,"RRInitialOuterWear":null,"RRInitialOuterMiddleWear":null,"RRInitialMiddleWear":null,"RRInitialInnerMiddleWear":null,"RRInitialInnerWear":null,"RRAfterOuterWear":null,"RRAfterOuterMiddleWear":null,"RRAfterMiddleWear":200,"RRAfterInnerMiddleWear":null,"RRAfterInnerWear":null,"LFInnerColdPressure":71,"LFInnerHotPressure":null,"RFInnerColdPressure":71,"RFInnerHotPressure":null,"LRInnerColdPressure":71,"LRInnerHotPressure":null,"RRInnerColdPressure":71,"RRInnerHotPressure":null,"LFColdPressure":61.5,"LFHotPressure":69.8,"RFColdPressure":61.5,"RFHotPressure":69.8,"LRColdPressure":61.5,"LRHotPressure":69.8,"RRColdPressure":61.5,"RRHotPressure":69.8,"TrackTemp":133}

Pero cuando entra en mi acción, el modelo es completamente nulo.

public JsonResult SaveTireRunModel(TireRunViewModel tirerun) { tirerun.Save(); return Json(new { success = true }); }

Mi clase de modelo es la siguiente

public class TireRunViewModel { public TireRunViewModel() { } public TireRunViewModel(Guid outingID) { //data access crud here } public string Comments { get; set; } public List<SelectBoxModel> TireSetsList { get; set; } public double? LFShoulderTemp { get; set; } public double? RFShoulderTemp { get; set; } public double? LFOuterTemp { get; set; } public double? LFMiddleTemp { get; set; } public double? LFInnerTemp { get; set; } public double? RFOuterTemp { get; set; } public double? RFMiddleTemp { get; set; } public double? RFInnerTemp { get; set; } public double? LROuterTemp { get; set; } public double? LRMiddleTemp { get; set; } public double? LRInnerTemp { get; set; } public double? RROuterTemp { get; set; } public double? RRMiddleTemp { get; set; } public double? RRInnerTemp { get; set; } public double? LFInitialOuterWear { get; set; } public double? LFInitialOuterMiddleWear { get; set; } public double? LFInitialMiddleWear { get; set; } public double? LFInitialInnerMiddleWear { get; set; } public double? LFInitialInnerWear { get; set; } public double? LFAfterOuterWear { get; set; } public double? LFAfterOuterMiddleWear { get; set; } public double? LFAfterMiddleWear { get; set; } public double? LFAfterInnerMiddleWear { get; set; } public double? LFAfterInnerWear { get; set; } public double? RFInitialOuterWear { get; set; } public double? RFInitialOuterMiddleWear { get; set; } public double? RFInitialMiddleWear { get; set; } public double? RFInitialInnerMiddleWear { get; set; } public double? RFInitialInnerWear { get; set; } public double? RFAfterOuterWear { get; set; } public double? RFAfterOuterMiddleWear { get; set; } public double? RFAfterMiddleWear { get; set; } public double? RFAfterInnerMiddleWear { get; set; } public double? RFAfterInnerWear { get; set; } public double? LRInitialOuterWear { get; set; } public double? LRInitialOuterMiddleWear { get; set; } public double? LRInitialMiddleWear { get; set; } public double? LRInitialInnerMiddleWear { get; set; } public double? LRInitialInnerWear { get; set; } public double? LRAfterOuterWear { get; set; } public double? LRAfterOuterMiddleWear { get; set; } public double? LRAfterMiddleWear { get; set; } public double? LRAfterInnerMiddleWear { get; set; } public double? LRAfterInnerWear { get; set; } public double? RRInitialOuterWear { get; set; } public double? RRInitialOuterMiddleWear { get; set; } public double? RRInitialMiddleWear { get; set; } public double? RRInitialInnerMiddleWear { get; set; } public double? RRInitialInnerWear { get; set; } public double? RRAfterOuterWear { get; set; } public double? RRAfterOuterMiddleWear { get; set; } public double? RRAfterMiddleWear { get; set; } public double? RRAfterInnerMiddleWear { get; set; } public double? RRAfterInnerWear { get; set; } public double? LFInnerColdPressure { get; set; } public double? LFInnerHotPressure { get; set; } public double? RFInnerColdPressure { get; set; } public double? RFInnerHotPressure { get; set; } public double? LRInnerColdPressure { get; set; } public double? LRInnerHotPressure { get; set; } public double? RRInnerColdPressure { get; set; } public double? RRInnerHotPressure { get; set; } public double? LFColdPressure { get; set; } public double? LFHotPressure { get; set; } public double? RFColdPressure { get; set; } public double? RFHotPressure { get; set; } public double? LRColdPressure { get; set; } public double? LRHotPressure { get; set; } public double? RRColdPressure { get; set; } public double? RRHotPressure { get; set; } public int? SetNumberLF { get; set; } public int? SetNumberRF { get; set; } public int? SetNumberLR { get; set; } public int? SetNumberRR { get; set; } public double? TrackTemp { get; set; } public void Save() { //data access crud here } }

¿Qué puedo hacer para seguir solucionando este problema y averiguar por qué mi modelo no es vinculante?


El contentType predeterminado para el método $ .ajax es ''application/x-www-form-urlencoded''

Si desea enviar JSON, debe especificar que está enviando JSON, de lo contrario, la Carpeta del modelo no funcionará correctamente:

$.ajax({ type: ''post'', url: "@Url.Action("SaveTireRunModel", "Events")", data: tirerun, contentType: ''application/json'', success: function(result) { if (!result.success) { alert(result.error); } else {} } });

Es posible que también necesite "codificar" los datos con: data: JSON.stringify(tirerun)


Tuve prácticamente el mismo problema: lo resolví usando ko.mapping.toJS(myVm) lugar de ko.mapping.toJSON(myVm) . Cuando utilizo TOJSON, todas las propiedades de vm se envían en una sola cadena concatenada, pero utilizando toJS, cada propiedad se recibe por separado en mi controlador, y se asigna correctamente a mi C # Viewmodel, en su caso la clase TireRunViewModel .