传递两种型号的控制器使用Ajax BeginForm() [英] Passing two models to Controller using Ajax BeginForm()

查看:211
本文介绍了传递两种型号的控制器使用Ajax BeginForm()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样的:

@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的是这样的:

and method in MyController like this:

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

    }

但对象的角色不过去了,无论是user.UserRole。

but object role is not passed, either 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; }
    }
}

所以,我怎样才能通过模型​​是这样,这里面有其他的引用类型?

so, How can I pass models like this, which has other reference types inside?

推荐答案

在您查看下面的行毫无意义

The following line in your view make no sense

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

UserRole 是一个复杂的对象,这取决于你是否已经覆盖了的ToString()方法,将呈现&LT;输入...值=MVCApp.Models.UserRole/&GT; 所以,当这回发了 DefaultModelBinder 正在尝试做 model.UserRole =MVCApp.Models.UserRole这当然失败,因此性能是

UserRole is a complex object, and depending on whether you have overridden its .ToString() method, it will render <input ... value="MVCApp.Models.UserRole" /> so when this posts back the DefaultModelBinder is trying to do model.UserRole = "MVCApp.Models.UserRole" which of course fails and the property is therefore null

删除它,而是绑定到 UserRole 属性要调回 - 因为你做了与 @ Html.TextBoxFor(M =&GT; Model.UserRole.Role)。例如 @ Html.HiddenFor(M =&GT; Model.UserRole.UserRoleID),但你似乎已经已绑定这与 @ Html.HiddenFor( M =&GT;。Model.UserRoleID)所以它可能不是necessay重蹈覆辙

Remove it, and instead bind to the properties of UserRole that you want posted back - as you have done with @Html.TextBoxFor(m => Model.UserRole.Role). For example @Html.HiddenFor(m => Model.UserRole.UserRoleID) but you already seem to have bound this with @Html.HiddenFor(m => Model.UserRoleID) so it might not be necessay to repeat it.

这篇关于传递两种型号的控制器使用Ajax BeginForm()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆