
public class Notificationviewmodel{ public string GroupDesc { get; set; } public bool AM { get; set; } public bool PM { get; set; } public int MaxNotif { get; set; }}public class Settingsviewmodel{ public List<Notificationviewmodel> ListNotification { get; set; } public Settingsviewmodel() { ListNotification = new List<Notificationviewmodel>(); }} 我的看法 :
@model PortailT2A.Models.Settingsviewmodel@{ VIEwBag.Title = "Preferences"; Layout = "~/VIEws/Shared/_Layoutadmin.csHTML";}<h2>Preferences</h2>@using(HTML.BeginForm("Preferences","administrateur",FormMethod.Post)){ <table ID="settingstable"> <tr> <th>Groupe</th> <th></th> <th>AM</th> <th>PM</th> <th>limite de notifications</th> </tr> @for (int i = 0; i < Model.ListNotification.Count(); i++ ) { var notif = Model.ListNotification[i]; <tr> <td>@notif.GroupDesc </td> <td>Heure de notification</td> <td>@HTML.CheckBoxFor(u => notif.AM) </td> <td>@HTML.CheckBoxFor(u => notif.PM) </td> <td>@HTML.TextBoxFor(u => notif.MaxNotif)</td> </tr> <tr/> } </table> <input type ="submit" value="Sauvegarder" />} 我的httpGet方法填充我的viewmodel并返回它.
[httpGet] public ActionResult Preferences(long IDUser) { context = new MainDatabaseEntitIEs(); List<Notificationviewmodel> notifications = new List<Notificationviewmodel>(); Settingsviewmodel settings = new Settingsviewmodel(); //Population... return VIEw(settings); } 但是,当我想保存更改时,我得到一个nullM的viewmodel,我不明白为什么.伙计们好吗?
编辑:我的帖子方法:
[httpPost] public ActionResult Preferences(Settingsviewmodel sm) { //since here my viewmodel is null context = new MainDatabaseEntitIEs(); Utilisateur user = (from u in context.Utilisateurs where u.Username == User.IDentity.name select u).FirstOrDefault(); //operations...} 生成的HTML:
<tr> <td>Groupe B </td> <td>Heure de notification</td> <td><input ID="notif_AM" name="notif.AM" type="checkBox" value="true" /><input name="notif.AM" type="hIDden" value="false" /> </td> <td><input checked="checked" ID="notif_PM" name="notif.PM" type="checkBox" value="true" /><input name="notif.PM" type="hIDden" value="false" /> </td> <td><input ID="notif_MaxNotif" name="notif.MaxNotif" type="text" value="10" /></td> </tr>解决方法 列表与LT; T>模型绑定时可能会很棘手,因为它严重依赖于索引键.帮助者需要知道索引,但是通过在for循环中分配notif,它们将丢失引用.相反,尝试以下内容:
@for (int i = 0; i < Model.ListNotification.Count(); i++ ){ var notif = Model.ListNotification[i]; <tr> <td>@notif.GroupDesc </td> <td>Heure de notification</td> <td>@HTML.CheckBoxFor(u => u.ListNotification[i].AM) </td> <td>@HTML.CheckBoxFor(u => u.ListNotification[i].PM) </td> <td>@HTML.TextBoxFor(u => u.ListNotification[i].MaxNotif)</td> </tr> <tr/>} 那应该为您提供如下内容:
<tr> <td>Groupe B </td> <td>Heure de notification</td> <td> <input ID="ListNotification[0]_AM" name="ListNotification[0].AM" type="checkBox" value="true" /> <input name="ListNotification[0].AM" type="hIDden" value="false" /> </td> <td> <input checked="checked" ID="ListNotification[0]_PM" name="ListNotification[0].PM" type="checkBox" value="true" /> <input name="ListNotification[0].PM" type="hIDden" value="false" /> </td> <td> <input ID="ListNotification[0]_MaxNotif" name="ListNotification[0].MaxNotif" type="text" value="10" /> </td></tr>
此外,请确保在已发布的 *** 作中检查ModelState.IsValID以确认模型已正确绑定.如果没有,您应该在ModelState中看到一个错误列表,它可以指示它可能失败的位置.
另外,我没有看到你将GroupDesc转储到任何地方(输出除外).如果在传入模型上这是必要的,您可以考虑使用@ HTML.HIDdenFor(x => x.ListNotifications [i] .GroupDesc).
总结以上是内存溢出为你收集整理的c# – ViewModel在HttpPost方法中为Null全部内容,希望文章能够帮你解决c# – ViewModel在HttpPost方法中为Null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)