我正在使用ASP.NET MVC 4,我构建了这些viewmodel:
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] 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).