0

How to create a List of values that to be referred by multiple models/screen?

I have created a model as ListValues, which will contains 2 fields (ID, Name) and I want that to be called by all of my models, what could be the best way. I do not want to loose on binding as well.

Abhijit
  • 3
  • 2

2 Answers2

0

Have all of your models inherit from ListValues. You might want to rename the ListValues, which is now a base class, to something more descriptive like ModelBase.

Here's an example:

ModelBase.cs

namespace ListValuesTest.Models
{
    public class ModelBase
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class Employee : ModelBase
    {
        public string Department { get; set; }
    }

    public class Customer : ModelBase
    {
        public string Application { get; set; }
    }
}

HomeController.cs

namespace ListValuesTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult EmployeeOfTheMonth()
        {
            ListValuesTest.Models.Employee NumberOneEmployee = new Models.Employee();
            NumberOneEmployee.ID = 1;
            NumberOneEmployee.Name = "Brian";
            NumberOneEmployee.Department = "IT";

            return View(NumberOneEmployee);
        }

        public ActionResult CustomerOfTheMonth()
        {
            ListValuesTest.Models.Customer NumberOneCustomer = new Models.Customer();
            NumberOneCustomer.ID = 1;
            NumberOneCustomer.Name = "Microsoft";
            NumberOneCustomer.Application = "Visual Studio";

            return View(NumberOneCustomer);
        }
    }
}

EmployeeOfTheMonth.cshtml

@model ListValuesTest.Models.Employee

@{
    ViewBag.Title = "EmployeeOfTheMonth";
}

<h2>EmployeeOfTheMonth</h2>

<fieldset>
    <legend>Employee</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Department)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Department)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

CustomerOfTheMonth.cshtml

@model ListValuesTest.Models.Customer

@{
    ViewBag.Title = "CustomerOfTheMonth";
}

<h2>CustomerOfTheMonth</h2>

<fieldset>
    <legend>Customer</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Application)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Application)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
sharpguru
  • 340
  • 1
  • 7
0

i think ViewModel will be your best solution,look on the web for solutions using any view model...if you face any difficulty,come to us then with code

Mir Gulam Sarwar
  • 2,588
  • 2
  • 25
  • 39
  • Hi Thanks for answer, can you please provide some reference to how to code the same. I have been struggling for last 2 days. Thanks in advance. – Abhijit Aug 20 '13 at 15:52
  • is your problem solved? Below is a link http://stackoverflow.com/questions/664205/viewmodel-best-practices or i can give you some demo code for viewmodel if you want – Mir Gulam Sarwar Aug 24 '13 at 07:23
  • Thanks janina, it will be of great help if you can share some code for viewmodal. I am still struggling. – Abhijit Aug 24 '13 at 10:13