简体   繁体   English

局部视图模型从主视图返回 null

[英]partial view model returns null from the main view

I'm trying to view two separate tables without relations between them in the same view.我试图在同一个视图中查看两个单独的表,而它们之间没有关系。 the idea is to create an account and add it to the account table and add multiple lobs and add it to the lob table.这个想法是创建一个帐户并将其添加到帐户表中,然后添加多个 lob 并将其添加到 lob 表中。 so I made two partial views for each one.所以我为每一个都做了两个局部视图。 I made a ViewModel that has an instance of the account and a list of the lob.我制作了一个 ViewModel,它有一个帐户实例和一个 lob 列表。 the problem is in the main view, the partial model returns null.问题出在主视图中,部分模型返回空值。 I don't know why.我不知道为什么。 any ideas?有任何想法吗? I appreciate your help.我感谢您的帮助。

@model Manage_account.Models.AccountVM

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
<div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div>
                @{Html.RenderPartial("~/Views/AccountOrOU/PartialViews/_Account.cshtml", Model);}
            </div>
            <div>
                @{Html.RenderPartial("~/Views/AccountOrOU/PartialViews/_Lob.cshtml", Model);}
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>

    </div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Partial view:部分观点:

@model Manage_account.Models.AccountOrOU


@using (Html.BeginForm())
{
    <div class="form-horizontal" id="ViewData">
        <h4>AccountOrOU</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">

            <div class="col-md-10">
                @Html.HiddenFor(model => model.AccountOrOUID, new { htmlAttributes = new { @class = "form-control" } })

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

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

}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Model:模型:

 public class AccountVM
    {
        public AccountOrOU AccountOrOU { get; set; }
        public List<Lob> lobs { get; set; }

    }

Controller:控制器:

 public ActionResult Create()
        {

            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(AccountVM vM)
        {if (vM != null)
            {
                var myAccount = db.AccountOrOUs.ToList();
                foreach (var acc in myAccount)
                {
                    acc.AccountOrOUID= vM.AccountOrOU.AccountOrOUID ;
                    acc.AccountOrOUName=vM.AccountOrOU.AccountOrOUName ;
                    acc.DepartmentID=vM.AccountOrOU.DepartmentID ;
                    db.AccountOrOUs.Add(acc);
                }
                db.SaveChanges();
            }


            return View("Index");
        }

I just removed the part ViewData.Model.AccountOrOU and added instead Model and it worked 👏 .我刚刚删除了部分ViewData.Model.AccountOrOU并添加了Model并且它起作用了 👏 。 but honestly, I don't know the difference here.但老实说,我不知道这里的区别。 the first one I used once and was working.第一个我用过一次并且正在工作。 I don't know why not working with this project?我不知道为什么不与这个项目合作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM