2766
2766

Reputation: 145

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. 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.

The problem is in the main view, the partial model returns null. I don't know why. What can I try next?

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

Upvotes: 2

Views: 968

Answers (1)

2766
2766

Reputation: 145

I just removed the part ViewData.Model.AccountOrOU and added instead Model and it worked 👏 . 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?

Upvotes: 1

Related Questions