C# Html.BeginForm向相关操作发送空参数(MVC 5)

C# Html.BeginForm向相关操作发送空参数(MVC 5),c#,asp.net-mvc,razor-2,C#,Asp.net Mvc,Razor 2,我试图有一个表单,在那里我编辑一个用户帐户,并在那里我添加使用所选插件的角色。为此,我创建了一个视图模型: public class UserAccountViewModel { public UserAccount UserAccount { get; set; } public MultiSelectList RolesSelectList { get; set; } public UserAccountViewModel() { } public UserA

我试图有一个表单,在那里我编辑一个用户帐户,并在那里我添加使用所选插件的角色。为此,我创建了一个视图模型:

public class UserAccountViewModel
{
    public UserAccount UserAccount { get; set; }
    public MultiSelectList RolesSelectList { get; set; }
    public UserAccountViewModel() { }
    public UserAccountViewModel(UserAccount userAccount, List<RolesAccount> AllRoles)
    {
        UserAccount = userAccount;
        RolesSelectList = new MultiSelectList(AllRoles, "Id", "Name", UserAccount.Roles.Select(r=>r.Id).ToList());
    }
} // class UserAccountViewModel
我认为我没有正确理解绑定机制。到目前为止,我的经验是模型是自动填充的,但在这种情况下,有什么东西把它搞砸了

为什么我会收到一个空参数?有人能给我指一下正确的方向吗?我读了很多文章,但一无所获


如何获取我正在查找的数据,在本例中,这些数据由UserAccount类表示,包括角色?我可以使用其他方法检索数据,但我更喜欢使用所选的插件。

在与此问题斗争了三天之后,我开始在另一台电脑上工作,原因与此问题无关,一切又开始工作。就VisualStudio版本和使用的分支而言,这两台PC之间没有区别。在另一个控制器上做了一些代码更改后,返回到初始PC,程序也开始在那里工作。

在与此问题斗争了三天之后,我开始在另一台PC上工作,原因与此问题无关,一切都重新开始工作。就VisualStudio版本和使用的分支而言,这两台PC之间没有区别。在另一个控制器上做了一些代码更改后,返回到初始PC,程序也开始在那里工作。

public ActionResult Edit([FromBody]UserAccountViewModel useraccount)
您是否在Get Action方法中将模型传递到视图?@Moe,我使用的是Post方法。@Chet,我试过了,但我不能用FromBody。谢谢。你不能是什么意思?它是否给出了编译错误,或者添加它并不能解决您的问题?
public ActionResult Edit([FromBody]UserAccountViewModel useraccount)
您是否在Get Action方法中将您的模型传递到视图?@Moe,我正在使用Post方法。@Chet,我尝试过,但无法使用FromBody。谢谢。你不能是什么意思?它是给出了一个编译错误,还是添加它并不能解决您的问题?
// Most of the members are stripped for clarity
public class UserAccount
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id{ get; set; }        
    public string Username{ get; set; }       
    public string Email{ get; set; }
    public string Password{ get; set; }
    public virtual ICollection<RolesAccount> Roles { get; set; }
 } // end class UserAccount
@model LoginModule.UserAccountViewModel
...
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>UserAccount</h4>
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.UserAccount.Id)

    <div class="form-group">
        @Html.LabelFor(model => model.UserAccount.Username, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserAccount.Username)
            @Html.ValidationMessageFor(model => model.UserAccount.Username)
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Roles", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.ListBox("RolesOfUser",
                                Model.RolesSelectList,
                                new
                                {
                                    @class = "chzn-select",
                                    data_placeholder = "Choose roles...",
                                    style = "width:350px;"
                                }) 
        </div>
    </div>

<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>
}
public ActionResult Edit(UserAccountViewModel useraccount)