Tra*_*ffy 6 c# asp.net-mvc entity-framework viewmodel
我正在使用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>();
}
}
Run Code Online (Sandbox Code Playgroud)
我的看法 :
@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" />
}
Run Code Online (Sandbox Code Playgroud)
我的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);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我想保存更改时,我得到一个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...
}
Run Code Online (Sandbox Code Playgroud)
生成的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>
Run Code Online (Sandbox Code Playgroud)
List<T>模型绑定时可能会很棘手,因为它严重依赖于索引键.帮助者需要知道索引,但是通过notif在for循环中分配它们会丢失引用.相反,尝试以下内容:
@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/>
}
Run Code Online (Sandbox Code Playgroud)
那应该为您提供如下内容:
<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>
Run Code Online (Sandbox Code Playgroud)
此外,请务必检查已ModelState.IsValid发布的操作以确认模型已正确绑定.如果没有,你应该看到一个错误列表,ModelState它会告诉你它可能失败的地方.
另外,我没有看到你转储到GroupDesc任何地方(输出除外).如果在传入模型上有必要,您可以考虑使用@Html.HiddenFor(x => x.ListNotifications[i].GroupDesc).