我创建了一个局部视图,该视图应显示带有复选框的用户列表,因此我可以在各个页面中重用此局部视图。问题是,我无法生成正确的 htmlprefix 输入(我想删除 . 的前缀)
模型:
public class CircleEditViewModel
{
[Key]
public int CircleId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
public bool IsSystem { get; set; }
public List<SimpleUserListViewModel> Users { get; set; }
public CircleEditViewModel()
{
Users = new List<SimpleUserListViewModel>();
}
}
public class SimpleUserListViewModel
{
public SimpleUserListViewModel()
{
}
public SimpleUserListViewModel(User user)
{
this.UserId = user.UserId;
FullName = user.FullName;
}
public int UserId { get; set; }
public byte[] Picture { get; set; }
public string FullName { get; set; }
public bool IsCheckedForAction { get; set; }
}
“主视图”:
@model Wims.Website.ViewModels.CircleEditViewModel
<script type="text/javascript">
$(document).ready(function () {
$.validator.unobtrusive.parse('form');
});
</script>
@using (Ajax.BeginForm(Html.ViewContext.RouteData.Values["Action"].ToString(), null, new AjaxOptions { HttpMethod = "POST", OnSuccess = "SaveDone(data)" }, new { id = "editform" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Circle</legend>
@Html.Label(DateTime.Now.ToString());
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</fieldset>
@Html.Partial("~/Views/Shared/_UserList.cshtml", Model.Users,
new ViewDataDictionary(Html.ViewDataContainer.ViewData)
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Users" }
})
@Html.GenerateSecureDataControls(model => model.CircleId)
<input type="submit" value="Save" />
}
部分观点:
@model List<Wims.Website.ViewModels.Shared.SimpleUserListViewModel>
@{
if (Model != null)
{
for (int i = 0; i < Model.Count; i++)
{
<div class="userDetail">
<div>
<div>
@Html.CheckBoxFor(model => model[i].IsCheckedForAction)
</div>
<div class="iconDiv">
@Html.Image("~/Content/Images/defaultUser.jpg", Model[i].FullName, null)
</div>
<div>
@Html.TextBoxFor(model => model[i].FullName)
@Html.HiddenFor(model => model[i].UserId)
</div>
</div>
</div>
<div style="clear: both"></div>
}
}
}
我快到了,输入生成的 id 是
id="Users.[0].FullName
有什么办法可以去掉第一个点?我昨天在博客上找到了一些解决方案(我再也找不到了......)但它是针对 MVC3 的,无论如何我都无法让它工作......
谢谢您的帮助!
编辑: 也许我应该使用 EditorFor 而不是部分视图: .NET MVC 4 Strongly typed ViewModel contains Strongly typed Model with EditorFor and EditorTemplate partial view not binding 今晚检查