本文介绍了[HttpPost]部分视图返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 嗨! 我有一些困难时间将数据传递给Controller中的部分视图httpPost ActionResult方法。 让我解释一下: 1.有我用Entity Framework ORM生成的实体,看起来像这样。Hi!I have some difficult time to pass data to Partial view httpPost ActionResult method in Controller.Let me explain:1.Have entity which i have been generated with Entity Framework ORM and this looks like this.<pre lang="c#">//------------------------------------------------------------------------------// <auto-generated>// This code was generated from a template.//// Manual changes to this file may cause unexpected behavior in your application.// Manual changes to this file will be overwritten if the code is regenerated.// </auto-generated>//------------------------------------------------------------------------------namespace Brasken.Entities{ using System; using System.Collections.Generic; public partial class bras_AllaCarte { public int AllaCarteID { get; set; } public string AllCarteInformation { get; set; } }} 2.我有MenyAdminController以及以下方法:2. I have MenyAdminController with folowing methods:public ActionResult RooleThem() { return View(); } [HttpGet , ChildActionOnly] public ActionResult _AllaCarte(int id=1) { var bras_allacarte = db.bras_AllaCarte.ToList(); return PartialView("_AllaCarte", bras_allacarte); } [HttpPost] [ValidateInput(false)] public ActionResult _AllaCarte(FormCollection formData, bras_AllaCarte model) { //var model = TempData["tempAllaCarte"] as bras_AllaCarte; if (ModelState.IsValid) { try { db.Entry(model).State = EntityState.Modified; db.SaveChanges(); return PartialView("_AllaCarte", model); } catch (DataException) { ModelState.AddModelError("", "Kunde inte updatera,Prova igen om problemet uppstår igen var god kontakta administratoren."); } } return View(); } 这种方法的海豚是用来做它们的通过局部视图_AllACarte.cshtml管理brass_AllaCarte实体,看起来如下: b $ bThe porpoise of this methods are to use them for administration of brass_AllaCarte entity thru partial view _AllACarte.cshtml , and this looks as folowing: @model IEnumerable<Brasken.Entities.bras_AllaCarte> <div class="jumbotron"> @using (Html.BeginForm("_AllaCarte", "MenyAdmin" , FormMethod.Post, null)) { var item = Model.First(); @Html.HiddenFor(modelItem => item.AllaCarteID) @Html.TextAreaFor(modelItem => item.AllCarteInformation); <ul> <li><a önclick="updateAllaCarteMeny()" class="arrow">Updatera</a></li> </ul> <input style="display:none" id="updateAllaCarte" type="submit" value="" /> }</div> 有RollThem.cshtml视图我在哪里调用Partial视图,这看起来像 这样:And att last i have RollThem.cshtml view where i calling a Partial view , and this lookslike this: <pre lang="xml">@{ ViewBag.Title = "RooleThem"; Layout = "~/Views/Shared/_Layout.cshtml";} @{Html.RenderAction("_AllaCarte", "MenyAdmin");}Problem:When i debug a project , im geting a right data , but when i need to save changes with HttpPost method model data is null.What im doing wrong?推荐答案在你的视图中更改 IEnumerable 到列表,这解决了我遇到的类似问题。In your view change the IEnumerable to List, that solved a similar issue I had.@model List<brasken.entities.bras_allacarte> IEnumerables是readonly。IEnumerables are readonly. 这篇关于[HttpPost]部分视图返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 18:01