如何访问MVC 4中的查看器在控制器中定义的变量

时间:2014-02-27 12:22:52

标签: asp.net-mvc-4

public class TestController : Controller
{

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

        return View(xmDTO );
    }}

我的控制器看起来像这样,我想从我的查看器访问列表  怎么做 ? 当我在循环中使用xmDTO

时,我不会来

2 个答案:

答案 0 :(得分: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
} 

答案 1 :(得分:0)

使用ViewBag将是一个很好的方法

控制器类

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>
    }