2013-08-19 20 views
0

如何創建要由多個模型/屏幕引用的值列表?填充來自多個模型的值列表

我已經創建了一個ListValues模型,它將包含2個字段(ID,Name),我希望被我的所有模型調用,可能是最好的方法。我也不想鬆懈。

回答

0

所有模型都從ListValues繼承。您可能希望將ListValues(現在是基類)重命名爲更類似於ModelBase的描述。

下面是一個例子:

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> 
+0

試圖這樣做,但得到的錯誤,請幫助一些代碼參考。在此先感謝 – Abhijit

0

我認爲視圖模型將是您最佳的解決方案,在網絡上看起來使用任何瀏覽模式的解決方案......如果你面對任何困難,到我們這裏來,然後用代碼

+0

嗨感謝您的回答,請你提供一些參考如何編碼相同。我一直在掙扎最後2天。提前致謝。 – Abhijit

+0

是你的問題解決?下面是一個鏈接http://stackoverflow.com/questions/664205/viewmodel-best-practices或者我可以給你一些演示代碼viewmodel如果你想 –

+0

謝謝janina,這將是非常有幫助的,如果你可以分享一些代碼視圖。我仍在掙扎。 – Abhijit