【问题标题】:Passing two models to Controller using Ajax BeginForm()使用 Ajax BeginForm() 将两个模型传递给控制器
【发布时间】:2014-12-17 19:12:36
【问题描述】:

我有这样的看法:

@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; }
    }
}

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

【问题讨论】:

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


    【解决方案1】:

    您认为以下行没有意义

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

    UserRole 是一个复杂的对象,取决于你是否覆盖了它的.ToString() 方法,它会渲染&lt;input ... value="MVCApp.Models.UserRole" /&gt; 所以当这个回发时DefaultModelBinder 正在尝试做model.UserRole = "MVCApp.Models.UserRole" 这当然会失败因此该属性为null

    删除它,而是绑定到您想要回发的UserRole 的属性——就像您对@Html.TextBoxFor(m =&gt; Model.UserRole.Role) 所做的那样。例如@Html.HiddenFor(m =&gt; Model.UserRole.UserRoleID),但您似乎已将其与@Html.HiddenFor(m =&gt; Model.UserRoleID) 绑定,因此可能无需重复。

    【讨论】:

      【解决方案2】:

      您可以创建自己的提交机制。只需使用$.ajax 并传入它的数据属性所有你的值。更多的 js 代码,但更灵活。

      【讨论】:

      • 这没有回答问题,而Ajax.BeginForm() ajax!
      猜你喜欢
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2013-06-03
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多