Html.BeginForm()无法正确呈现

时间:2014-05-31 03:02:21

标签: c# asp.net-mvc razor html.beginform

在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; }
    }

****编辑****

我得到这个表格

My current form.

http://i834.photobucket.com/albums/zz268/gtas/formmine_zpsf6470e02.png

我应该收到一个像我这样复制代码的表格

How it should look copied from the tutorial

http://i834.photobucket.com/albums/zz268/gtas/formcopied_zpsdb2f129e.png

任何想法在哪里或如何看待邪恶的根源现在让我的生活变得艰难?

2 个答案:

答案 0 :(得分:1)

您尚未为组定义编辑器,行@Html.EditorFor(model => model.Groups)尝试查找用于呈现属性的编辑器,然后使用默认值,而不是您想要的。

您需要定义一个知道如何呈现该属性的编辑器模板,或者您需要使用for循环在页面内呈现它。

答案 1 :(得分:1)

尝试替换此

@Html.EditorFor(model => model.Groups)

使用此方法(生成包含Selected属性的复选框的表格行和GroupName属性的标签)

@foreach(var group in Model.Groups)
{
   <tr>
    <td>
      Html.CheckBoxFor(g => group.Selected)
    </td>
    <td>
      <span>group.GroupName</span>
    </td>
   </tr>

}