简体   繁体   中英

ASP.net MVC/EF/C# add none related table records to query in controller

I am trying to add records from table position for positionName(s) to let user select a position for employee when editing.My last attempts is to add a navigation property like field in company model

public virtual ICollection<Position> Mpositions { get; set; }

But all I got so far is null ref exception or no element in viewModel with property "PositionName" ass per viewbag didn't bother using everybody keeps recommending to avoid it so not going to do so either.

public ActionResult Edit([Bind(Include = "CompanyID,CompanyName,EntityForm,Address,Dissolute,CreationDate,FiscaleYear,Description")] Company company)
        {
            var GlbUpdate = db.Companies.Include(c => c.Members).Include(p => p.Mpositions);
            List<Company> mdlCompanies = new List<Company>();
            foreach (var item in GlbUpdate)
            {

                if ((item.Mpositions==null) || (item.Mpositions.Count() == 0))
                {
                    item.Mpositions = (ICollection<Position>)new SelectList(db.Positions.Except((IQueryable<Position>)db.Positions.Select(xk => xk.Members)), "PositionID", "PositionName");
                }
                mdlCompanies.Add(item);
//I tried first to edit the Mpositions property directly in gblUpdate
                //item.Mpositions = (IEnumerable<Position>)db.Positions.Select(p => new SelectListItem { Value = p.PositionID.ToString(), Text = p.PositionName}) ;
                //(ICollection<Position>)db.Positions.ToListAsync();
            }

In the view I have this

List<SelectListItem> mPositionNames = new List<SelectListItem>();
@*@this yields no results if I try gettign it from the compani record itself it gives a logic error where all id match all positionNames impossible to select an item and only positions already registered are available on the dropDownlist*@
@{foreach (var item in Model.Mpositions)
  {

    mPositionNames.Add(new SelectListItem() { Text = item.PositionName, Value = item.PositionID.ToString(), Selected = (false) ? true : false });
@*@selected attribute set to false not an issue, no data to select from :p so far*@
  }
}
@*@null exception(if i try to midify Mpositions directly in controler) here or empty list if modify it then put it with original query in a new list*@
<div class="SectionContainer R-sectionContainerData" id="MwrapperDataRight">
@Html.DropDownListFor(mpos => item.PositionID, (SelectList)Model.Mpositions)
</div>

All I want to do is pull the positions table to create a drop downList so users can change the position of an employee but since position has a 1>many relation with employee not companies it is not bound automatically by EF nor I seem to be able to use Include() to add it.

Your query for editing positions are complex. This query must edit person's info only. Using Edit action for formalizing position's edit are not correct.It's againts to Single Responsibility Principle. Use ViewComponents for this situation. Load positions separately from person info.

我找到了一个合适的解决方案,该模型使用封装其他实体的模型,然后使用Partialviews / RenderAction,以便每个部分处理一个实体/操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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