使用Ajax BeginForm()将两个模型传递给Controller

时间:2014-12-17 19:12:37

标签: c# ajax asp.net-mvc entity-framework

我有这样的观点:

@model MVCApp.Models.User

@{
    ViewBag.Title = "EditUser";
}

<h2>Edycja użytkownika</h2>

@using (Ajax.BeginForm("SaveUser", "My", new AjaxOptions { UpdateTargetId = "Result" }))
{
    <fieldset>
        <legend>Zmień dane użytkownika</legend>
        <div id="EditUserForm">
            <div>
                @Html.LabelFor(m => Model.Login)
                @Html.TextBoxFor(m => Model.Login)
            </div>
            <div>
                @Html.LabelFor(m => Model.Password)
                @Html.TextBoxFor(m => Model.Password)
            </div>
            <div>
                @Html.LabelFor(m => Model.Name)
                @Html.TextBoxFor(m => Model.Name)
            </div>
            <div>
                @Html.LabelFor(m => Model.Surname)
                @Html.TextBoxFor(m => Model.Surname)
            </div>
            <div>
                @Html.LabelFor(m => Model.UserRole.Role)
                @Html.TextBoxFor(m => Model.UserRole.Role)
            </div>
            <input type="submit" value="Zapisz zmiany" />
            @Html.HiddenFor(m => Model.UserRole)
            @Html.HiddenFor(m => Model.UserRoleID)
            @Html.HiddenFor(m => Model.UserID)
        </div>
    </fieldset>
    <div id="Result"></div>
    @Html.ValidationSummary(true)
}
MyController中的

和方法如下:

    [HttpPost]
    public ActionResult SaveUser(User user, UserRole role)
    {
        //code here

    }

但是没有传递对象角色,user.UserRole。

我的用户模型类:

namespace MVCApp.Models
{

    public partial class User
    {
        public User()
        {
            this.Factures = new HashSet<Facture>();
            this.Settings = new HashSet<Setting>();
            this.Companies = new HashSet<Company>();
        }

        public int UserID { get; set; }
        public Nullable<int> UserRoleID { get; set; }
        public string Login { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }

        public virtual ICollection<Facture> Factures { get; set; }
        public virtual ICollection<Setting> Settings { get; set; }
        public virtual UserRole UserRole { get; set; }
        public virtual ICollection<Company> Companies { get; set; }
    }
}

角色模型类:

namespace MVCApp.Models
{

    public partial class UserRole
    {
        public UserRole()
        {
            this.Users = new HashSet<User>();
        }

        public int UserRoleID { get; set; }
        public string Role { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }
}

那么,我怎样才能传递这样的模型,其中包含其他引用类型?

2 个答案:

答案 0 :(得分:1)

视图中的以下行没有意义

@Html.HiddenFor(m => Model.UserRole)

UserRole是一个复杂的对象,根据您是否覆盖了其.ToString()方法,它会呈现<input ... value="MVCApp.Models.UserRole" />所以当这个帖子发回时,DefaultModelBinder正在尝试当然model.UserRole = "MVCApp.Models.UserRole"失败,因此属性为null

删除它,然后绑定到您想要发回的UserRole属性 - 就像使用@Html.TextBoxFor(m => Model.UserRole.Role)一样。例如@Html.HiddenFor(m => Model.UserRole.UserRoleID),但您似乎已经将其与@Html.HiddenFor(m => Model.UserRoleID)绑定在一起,因此重复它可能不是必需的。

答案 1 :(得分:0)

您可以创建自己的提交机制。只需使用$.ajax并将所有值传递给其data属性即可。多一点js代码,但更灵活。

相关问题