在stackoverflow中搜索时,其他问题对我的情况并没有帮助。如何调试此类错误,例如 Html.BeginForm 未正确呈现到页面的错误。
我使用此代码
@model ExtremeProduction.Models.SelectUserGroupsViewModel
@{ ViewBag.Title = "User Groups"; }
<h2>Groups for user @Html.DisplayFor(model => model.UserName)</h2>
<hr />
@using (Html.BeginForm("UserGroups", "Account", FormMethod.Post, new { encType = "multipart/form-data", id = "userGroupsForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
<div class="col-md-10">
@Html.HiddenFor(model => model.UserName)
</div>
</div>
<h4>Select Group Assignments</h4>
<br />
<hr />
<table>
<tr>
<th>
Select
</th>
<th>
Group
</th>
</tr>
@Html.EditorFor(model => model.Groups)
</table>
<br />
<hr />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
编辑:添加了模型
// Wrapper for SelectGroupEditorViewModel to select user group membership:
public class SelectUserGroupsViewModel
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<SelectGroupEditorViewModel> Groups { get; set; }
public SelectUserGroupsViewModel()
{
this.Groups = new List<SelectGroupEditorViewModel>();
}
public SelectUserGroupsViewModel(ApplicationUser user)
: this()
{
this.UserName = user.UserName;
this.FirstName = user.FirstName;
this.LastName = user.LastName;
var Db = new ApplicationDbContext();
// Add all available groups to the public list:
var allGroups = Db.Groups;
foreach (var role in allGroups)
{
// An EditorViewModel will be used by Editor Template:
var rvm = new SelectGroupEditorViewModel(role);
this.Groups.Add(rvm);
}
// Set the Selected property to true where user is already a member:
foreach (var group in user.Groups)
{
var checkUserRole =
this.Groups.Find(r => r.GroupName == group.Group.Name);
checkUserRole.Selected = true;
}
}
}
// Used to display a single role group with a checkbox, within a list structure:
public class SelectGroupEditorViewModel
{
public SelectGroupEditorViewModel() { }
public SelectGroupEditorViewModel(Group group)
{
this.GroupName = group.Name;
this.GroupId = group.Id;
}
public bool Selected { get; set; }
[Required]
public int GroupId { get; set; }
public string GroupName { get; set; }
}
public class Group
{
public Group()
{
}
public Group(string name)
: this()
{
Roles = new List<ApplicationRoleGroup>();
Name = name;
}
[Key]
[Required]
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<ApplicationRoleGroup> Roles { get; set; }
}
**** 编辑 ****
我得到了这个表格
http://i834.photobucket.com/albums/zz268/gtas/formmine_zpsf6470e02.png
我应该收到一份像我这样复制代码的表格
http://i834.photobucket.com/albums/zz268/gtas/formcopied_zpsdb2f129e.png
任何想法在哪里或如何寻找使我的生活变得艰难一段时间的邪恶根源?