本文介绍了为什么我的模型会传递null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好 为什么我的模型会传递null? 用户名,Tell,Mobil,......不会填写控制器。他们是我模型的字段(Hiddenfor) 。 当我在脚本块中发出警报时,我看到它们正确。 但不会传递给控制器​​。 i认为控制器中的ViewModelX是不同于脚本块中的jsonMyModel。 如何在它们之间建立关系? < script type = text / javascript> $( #submit-btn2)。click( function (){saveMyModel();}); function saveMyModel(){ var e = document .getElementById( id_Send); var str = e.options [e.selectedIndex] .value; var e2 = document .getElementById( id_Receive); var str2 = e.options [e.selectedIndex] .value; alert(str); alert(str2); alert($( 。tdRecievername)。html()) ; alert($( 。tdMobile)。html()); $ .ajax({ url:' @ Url.Action(保存,主页)',类型:' POST', contentType:' application / json',数据: JSON .stringify({ jsonMyModel:{ username:$( 。tdBuyername)。html(), Tell:$( 。tdPhone)。html(),名称:$( 。tdRecievername)。html(), Mobil:$( 。tdMobile)。html(), Lname:$( 。tdLname)。html(), ShFactor:$( #Sh-Factor)。html(),地址:$( 。tdAddress)。html(), Cod:$( 。tdPobox)。html(), id_send:str, id_Receive:str2, },成功:功能(数据){ alert(' im back'); alert(data); },错误: function (xhr,ajaxOptions,error){ alert(xhr.status); alert(' 错误:' + xhr.responseText); } }), }); } < / script> 控制器: [HttpPost] public ActionResult save(ViewModelX jsonMyModel) { var data = true ; if (ModelState.IsValid) { result = HelperClass.Insert(jsonMyModel.Address,jsonMyModel.Cod , jsonMyModel.ShFactor,jsonMyModel.id_send, jsonMyModel.idNoePardakht_sefaresh,jsonMyModel.id_Receive,jsonMyModel.Lname,jsonMyModel.Mobil,jsonMyModel.Name,jsonMyModel.Tell,jsonMyModel.username); } else { data = 假; } return Json(数据); } 解决方案 ( #submit-btn2)。click( function (){saveMyModel();}); function saveMyModel(){ var e = document .getElementById( id_Send); var str = e.options [e.selectedIndex] .value; var e2 = document .getElementById( id_Receive); var str2 = e.options [e.selectedIndex] .value; alert(str); alert(str2); alert( ( 。tdRecievername )HTML())。 alert( ( 。tdMobile )。html的()); hellowhy my model will pass null?username , Tell,Mobil ,... will not fill in controller .they are my model's fields(Hiddenfor).when i make alert in script block , i see them correctly.but will not pass to controller.i think ViewModelX in controller is diffrent to jsonMyModel in script block.how can i make a relation between them? <script type="text/javascript"> $("#submit-btn2").click(function () {saveMyModel(); }); function saveMyModel() { var e = document.getElementById("id_Send"); var str = e.options[e.selectedIndex].value; var e2 = document.getElementById("id_Receive"); var str2 = e.options[e.selectedIndex].value; alert(str); alert(str2); alert($(".tdRecievername").html()); alert($(".tdMobile ").html()); $.ajax({ url: '@Url.Action("Save", "Home")', type: 'POST', contentType: 'application/json', data: JSON.stringify({ jsonMyModel: { username: $(".tdBuyername").html(), Tell: $(".tdPhone").html(), Name: $(".tdRecievername").html(), Mobil: $(".tdMobile").html(), Lname: $(".tdLname").html(), ShFactor: $("#Sh-Factor").html(), Address: $(".tdAddress").html(), Cod: $(".tdPobox").html(), id_send: str, id_Receive:str2, }, success: function (data) { alert('im back'); alert(data); }, error: function (xhr, ajaxOptions, error) { alert(xhr.status); alert('Error: ' + xhr.responseText); } }), }); }</script>Controller:[HttpPost] public ActionResult save(ViewModelX jsonMyModel) { var data = true ; if (ModelState.IsValid) { result = HelperClass.Insert(jsonMyModel.Address, jsonMyModel.Cod, jsonMyModel.ShFactor, jsonMyModel. id_send, jsonMyModel.idNoePardakht_sefaresh, jsonMyModel.id_Receive, jsonMyModel.Lname, jsonMyModel.Mobil, jsonMyModel.Name, jsonMyModel.Tell, jsonMyModel.username); } else { data = false; } return Json(data); } 解决方案 ("#submit-btn2").click(function () {saveMyModel(); }); function saveMyModel() { var e = document.getElementById("id_Send"); var str = e.options[e.selectedIndex].value; var e2 = document.getElementById("id_Receive"); var str2 = e.options[e.selectedIndex].value; alert(str); alert(str2); alert((".tdRecievername").html()); alert((".tdMobile ").html()); 这篇关于为什么我的模型会传递null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 16:38