2014-02-27 23 views
0
public class TestController : Controller 
{ 

    public ActionResult Index() 
    { 
      var xmDTO = XFacade.XList(); 

     return View(xmDTO); 
    }} 

我的控制器看起來像,我想從我的瀏覽器訪問 名單如何做到這一點? 我不會當我使用xmDTO在一個循環如何由觀衆訪問控制器定義varibales在MVC4

+0

你應該認真鍵入 – Amila

+0

UM使用剃刀所以它不會幫助,並在型號或viewbag&鑑於訪問它不MVC4風格 – Kalanamith

+0

傳遞的數據將是很好的做法您的看法 –

回答

0

使用ViewBag將是很好的方法

Controller類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace MvcApplication4.Controllers 
{ 
    public class HomeController : Controller 
    { 
     // 
     // GET: /Home/ 

     public ActionResult Index() 
     { 
      ViewBag.country = new List<string>() 
      { 
       "USA", 
       "INDIA", 
       "UK" 
      }; 
      return View(); 
     } 

    } 

} 

查看

@{ 
    ViewBag.Title = "List of string value"; 
} 


<h2>Country List</h2> 
<ul> 
    @foreach (string country in ViewBag .country) 
    { 
     <li>@country</li> 
    } 
+0

使用ViewBag永遠不會是好方法! – MDDDC

+0

@MDDDC你的方法是什麼? – Kalanamith

+0

@MDDDC然後強類型視圖是不錯的方法? –

2

爲什麼你不能強烈地鍵入視圖?

public class TestController : Controller 
{ 
    public ActionResult Index() 
    { 
     var xmDTO = XFacade.XList(); 

     return View(xmDTO); 
    } 
} 
在你看來

@model XList<type> 
@{ 
    ViewBag.Title = "List of string value"; 
} 


@foreach (var item in country in Model) 
{ 
    //use properties in item 
}