为什么mvc无法传递对象

时间:2012-09-14 10:35:39

标签: c# asp.net-mvc

无法弄清楚为什么我没有在控制器中接收对象。我从这里跟随示例:http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example

我看起来像这样:
CONTROLLER

private List<webpages_Roles> GetOptions()
    {
        List<webpages_Roles> categories = new List<webpages_Roles>();
        foreach (Models.webpages_Roles databaseRole in Securitydb.webpages_Roles.ToArray())
        {
            categories.Add(new webpages_Roles() { RoleName = databaseRole.RoleName, RoleId = databaseRole.RoleId });
        }        

        return categories;
    }

    public ActionResult Users2()
    {
        UserProfile user = new UserProfile();
        user.UserId = 24;
        user.UserName = "Alan";
        user.webpages_Roles.Add(new webpages_Roles { RoleName = "Administrator", RoleId = 1 });
        ViewBag.Categories = GetOptions();
        return View(user);
    }

    [HttpPost]
    public ActionResult Users2(UserProfile user)
    {
        ViewBag.Categories = GetOptions();
        return View(user);
    }

MODEL

public partial class webpages_Roles
{
    public webpages_Roles()
    {
        this.UserProfiles = new HashSet<UserProfile>();
    }

    public int RoleId { get; set; }
    public string RoleName { get; set; }

    public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
public partial class UserProfile
{
    public UserProfile()
    {
        this.webpages_Roles = new HashSet<webpages_Roles>();
    }

    public int UserId { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}

查看

@model Models.UserProfile

@{
    ViewBag.Title = "Users2";
}
<div>
<h1>Select Values</h1>
<ul>
@foreach (Models.webpages_Roles c in Model.webpages_Roles)
   {
   <li> @c.RoleId </li>
 }
</ul>
@Html.DisplayForModel()

@Html.HiddenFor(x => x.webpages_Roles)

<h1>Change Values</h1>
@using (Html.BeginForm())
   {
   @Html.ListBoxFor(x => x.webpages_Roles, new MultiSelectList(ViewBag.Categories, "RoleId", "RoleName", Model.webpages_Roles));
   <br />
   <input type="submit" value="Submit" />
 }
    </div>

选择列表中的更改并按提交按钮

public ActionResult Users2(UserProfile user)

用户为空。尝试使用隐藏字段为UserProfile的每个属性但行为没有变化。

1 个答案:

答案 0 :(得分:1)

在您的视图中,您需要围绕模型中的所有属性包装表单。因为当您提交表单时,它只获取ListBox值并且与您的UserProfile对象不匹配。所以做这样的事情:

@model GEMPlus.Models.UserProfile

@{
    ViewBag.Title = "Users2";
}
<div>
    <h1>Select Values</h1>
    @using (Html.BeginForm())
    {
        <ul>
            @foreach (GEMPlus.Models.webpages_Roles c in Model.webpages_Roles)
            {
                <li> @c.RoleId </li>
            }
        </ul>
        @Html.DisplayForModel()

        @Html.HiddenFor(x => x.webpages_Roles)
            @Html.HiddenFor(x => x.RoleId)

        <h1>Change Values</h1>
        @Html.ListBoxFor(x => x.webpages_Roles, new MultiSelectList(ViewBag.Categories, "RoleId", "RoleName", Model.webpages_Roles));
        <br />
        <input type="submit" value="Submit" />
    }
</div>

确保表单内至少有HiddenFor元素。 DisplayFor不会产生太大的影响。还要将RoleId包含在表单中隐藏。

我无法确定您需要在表单中放置哪些确切元素,但问题出在该区域 - 提交的表单没有足够的信息将其映射到对象。