I want to call multiple model in my ADD view. I'm having hard time to call issues model and systems model in one view. See below codes
IssuesController
public ActionResult GetIssues()
{
using(SSD_INTERIM db = new SSD_INTERIM())
{
var issueList = db.TBL_ISSUES.Where(x => x.STATUS != "Closed").ToList();
return Json(new { data = issueList }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult GetSystems()
{
using(SSD_INTERIM db = new SSD_INTERIM())
{
var issueList = db.LIB_SSD_SYSTEMS.Where(x => x.ENABLED == "Y").ToList();
return Json(new { data = issueList }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddOrEdit(int id = 0)
{
return View(new TBL_ISSUES());
}
AddOrEdit.cshtml (view)
@model ProjectName.Models.TLB_ISSUES
@{
Layout = null;
}
@using (Html.BeginForm("AddOrEdit","Issues", FormMethod.Post, new { onsubmit = "return SubmitForm(this)"}))
{
@Html.HiddenFor(model => model.ISSUE_ID)
<div>
@Html.LabelFor(model => model.SYSTEMNAME, "System", new { @class = "control-label" })
@* Put DropdownListFor for system from different model *@
</div>
<div>
@Html.LabelFor(model => model.ISSUE_DESC, "Description", new { @class = "control-label" })
@Html.EditFor(model => model.ISSUE_DESC, new { htmlAttributes = new {@class="form-control"}})
</div>
}
Screenshot of Entity Framework model
Hope you can help me with my issue, I want to populate system dropdown with datas from different model.
CodePudding user response:
One option is to define a view model for your Add/Edit. I would recommend using a naming convention such as referring to that view as a "Detail" view rather than "AddOrEdit".
[Serializable]
public class IssueDetailViewModel
{
public IssueViewModel Issue { get; set; }
public ICollection<SystemSummaryViewModel> Systems { get; set; } = new List<SystemSummaryViewModel>();
}
[Serializable]
public class IssueViewModel
{
//.. relevant Issue fields matching what you will need to insert/update an Issue entity.
}
[Serializable]
public class SystemSummaryViewModel
{
public int SystemId { get; set; }
public string DisplayName { get; set; }
}
Then populate this model in the case of an Add:
var systems = db.LIB_SSD_SYSTEMS
.Where(x => x.ENABLED == "Y")
.Select(x => new SystemSummaryViewModel
{
SystemId = x.ID,
DisplayName = x.SYSTEMNAME
}).ToList();
var model = new IssueDetailViewModel
{
Issue = new IssueViewModel();
Sytems = systems
};
return View(model);
When you bind the controls in your view, the controls for the Issue use @model.Issue.{property}
while your selections for Systems are provided by @model.Systems
.
I do not recommend ever passing Entity classes to and from views, but instead get in the practice of defining view models. The model for a view is a separate concern to a data model (which the entity reflects). Passing entities around leads to all kinds of issues including risking exposing far more information about your data implementation than the client needs, sending more data over the wire than the client needs, performance issues arising from serialization and lazy loading, and vague errors occurring when working with detached entity instances. These errors are commonly due to missing data or multiple instances for the same data when deserialized from the client, and can be situational making them appear intermittently or otherwise hard to reproduce.
For larger models with many lookups, or cases where you have larger lookup lists that you'll want to implement something like auto-complete searching to find matches (addresses, etc.) then you'll likely want to look at performing Ajax calls back to the server to load lookups on demand or based on search criteria.