@ Html.DropDownListFor和viewmodel没有回发到控制器?

时间:2015-10-27 20:00:46

标签: c# asp.net-mvc dropdown

是时候问互联网了。我是一名学生,对MVC +编码一般都是新手,但我不能为我的生活弄清楚为什么这不起作用。可能是非常明显的事情。 :/

我有一个对视图模型(AddMemberToGroup)强类型的视图(PersonGroupingViewModel)。我想要做的是将一个人添加到一个组,用户从视图的下拉列表中选择一个组。

Viewmodel:

public class PersonGroupingViewModel
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set;}

    public List<Group> Memberships { get; set; }

    public SelectList AllGroups { get; set; }
    public int SelectedGroupId { get; set; }

}

控制器:

//GET People/AddToGroup
public ActionResult AddMemberToGroup(int? PersonId)
{
    if (PersonId == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    }

    Person person = db.People.Find(PersonId);
    if (person == null)
    {
        return HttpNotFound();
    }

    SelectList allthegroups = new SelectList(db.Groups, "GroupId", "Name");

    PersonGroupingViewModel viewmodel = new PersonGroupingViewModel();
    viewmodel.PersonId = person.PersonId;
    viewmodel.FirstName = person.FirstName;
    viewmodel.LastName = person.LastName;
    viewmodel.AllGroups = allthegroups;

    //viewmodel.Memberships cannot be empty
    if (person.Memberships == null)
    {
        viewmodel.Memberships = new List<Group>();
    }
    else 
    { 
        viewmodel.Memberships = person.Memberships.ToList(); 
    }

    return View(viewmodel);
}

//POST: People/AddToGroup 
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddMemberToGroup(PersonGroupingViewModel vm)
{
    SelectList allthegroups = new SelectList(db.Groups, "GroupId", "Name");
    vm.AllGroups = allthegroups; 

    int PersonId = vm.PersonId;
    int GroupId = vm.SelectedGroupId;

    Person person = db.People.Find(PersonId);
    Group group = db.Groups.Find(GroupId);
    group.Members.Add(person);
    db.SaveChanges();

    return View(vm);
}

查看表单(强类型为PersonGroupingViewModel):

将成员添加到组

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div>
    <h4>Groups @Html.DisplayFor(model => model.FirstName) is already in:</h4>
    <ul>
        @foreach (var group in Model.Memberships)
        {
            <li>@Html.DisplayFor(modelItem => group.Name)</li>
        }
    </ul>
</div>

<div class="form-horizontal">
    <h4>Add @Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName) To Group:</h4>

    @Html.HiddenFor(model => model.PersonId)

    <div class="form-group">
        <label>Group: </label>
        @Html.DropDownListFor(m => m.SelectedGroupId, Model.AllGroups, String.Empty)
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Add To Group" class="btn btn-default" />
        </div>
    </div>
</div>
}

当我点击“提交”时,在浏览器中我得到一个URL: localhost:54696/People/AddMemberToGroup?PersonId=1

除了异常&#34;对象引用未设置为对象的实例&#34;在控制器的Group group = db.Groups.Find(GroupId);行。

视图模型是否为空或根本没有?我不确定发生了什么,并希望有人试图用小词来解释它。我已经做了很多谷歌搜索,并尝试了几个建议,但最后我觉得好像我在圈子里。

1 个答案:

答案 0 :(得分:0)

解决了......

group.Members为null所以group.Members.Add(person)不起作用。如果列表为null,则在if / else语句中写入实例化新成员列表,并且它工作正常:

if (group.Members == null)
            {
                group.Members = new List<Person>();
                group.Members.Add(person);
            }
            else {
                group.Members.Add(person);
            }