c# – ViewModel在HttpPost方法中为Null
发布时间:2020-12-15 08:46:28 所属栏目:百科 来源:网络整理
导读:我正在使用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 SettingsView
我正在使用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方法填充我的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). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |