i have class name MasterModel in which two different classes
1: MasterModel
2: departmentofficialsList
I want deptname from departmentofficialsList
in a view how can call departmentofficialsList class
but in a view MasterModel is using and i want Authority Name
in Model.DeptName
model PPAApplication.Models.ViewModel.MasterModel
{
ViewBag.Title = "officermaster";
Layout = "~/Views/Shared/_Citizenlayout.cshtml";
}
using (Html.BeginForm("officermaster", "Master", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsForums
Top experts
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Jun 7, 2024, 11:33 PM EDT
Hello Garima,
There are several approaches you can use to achieve the solution.
public class MasterModel { public int Id { get; set; } public string AuthorityName { get; set; } public string DepartmentName { get; set; } }public class PPAApplicationViewModel { public MasterModel masterModel { get; set; } public DepartmentModel departmentModel { get; set; } // For One to One relationship //public List departmentModel { get; set; } // For One to Many relationship } public class MasterModel { public int Id { get; set; } public string AuthorityName { get; set; } } public class DepartmentModel { public int Id { get; set; } public string DepartmentName { get; set; } }Thanks
Vishal Joshi
Jignesh KumarRank #91 · 22.9K reputationPosted Jun 7, 2024, 7:41 AM EDT
Hello Garima,
You can create below model and view model for both model in single model,
public class MasterModel { public int Id { get; set; } public string Name { get; set; } // more properties if you have } public class Department { public int DepartmentId { get; set; } public string DepartmentName { get; set; } // more properties if you have }View model :
public class MasterViewModel { public MasterModel Master { get; set; } public List Departments { get; set; } }Sample Data which you need to bring from database,
public class HomeController : Controller { public ActionResult Index() { // Simulate fetching data from a database MasterModel master = new MasterModel { Id = 1, Name = "Master Name" // Add more properties as per your model }; List departments = new List { new Department { DepartmentId = 1, DepartmentName = "HR" }, new Department { DepartmentId = 2, DepartmentName = "IT" }, new Department { DepartmentId = 3, DepartmentName = "Finance" } // Add more properties as per your model }; MasterViewModel viewModel = new MasterViewModel { Master = master, Departments = departments }; return View(viewModel); } }Display in View :
@model YourNamespace.Models.MasterViewModel Master and Department List Master Details Id: @Model.Master.Id Name: @Model.Master.Name Departments @foreach (var department in Model.Departments) { @department.DepartmentName }