繁体   English   中英

如何将字典值从Controller传递到View?

[英]How do I pass the dictionary values from Controller to View?

我正在创建一个元素周期表,并在家庭控制器中创建了一个字典,名为dictionaryOfElements,该字典以元素的原子序号为键,元素的原子序号,符号,名称和权重为值。 我需要帮助将字典的所有值显示给ViewModel中的View。

以下是我创建的模型:

public class Elements
{
    public int AtomicNumber { get; set; }
    public string Symbol { get; set; }
    public string Name { get; set; }
    public string Weight { get; set; }

    public Elements()
    {
        //
    }

    public Elements(int atomic, string sym, string name, string weight)
    {
        this.AtomicNumber = atomic;
        this.Symbol = sym;
        this.Name = name;
        this.Weight = weight;
    }
}

我在其中声明字典的家庭控制器

public class HomeController : Controller
{
    Elements Hydrogen = new Elements(1, "H", "Hydrogen", "1.008");
    Elements Helium = new Elements(2, "He", "Helium", "4.0026");
    Elements Lithium = new Elements(3, "Li", "Lithium", "6.94");
    Elements Beryllium = new Elements(4, "Be", "Beryllium", "9.0122");
    Dictionary<int, Elements> dictionaryOfElements = new Dictionary<int, Elements>();

    // GET: Home
    public ActionResult DisplayElements()
    {          
        dictionaryOfElements.Add(Hydrogen.AtomicNumber, Hydrogen);
        dictionaryOfElements.Add(Helium.AtomicNumber, Helium);
        dictionaryOfElements.Add(Lithium.AtomicNumber, Lithium);
        dictionaryOfElements.Add(Beryllium.AtomicNumber, Beryllium);

        return View(dictionaryOfElements);
    }

    public ActionResult Index()
    {
        return View();
    }
}  

视图的视图模型

public class ElementsOfPeriodicTable
{
    public Elements elements { get; set; }
    public Dictionary<int, Elements> dictionaryOfElements { get; set; }
}

到目前为止,这就是我的视图的样子

    @model PeriodicTable.View_Model.ElementsOfPeriodicTable
@{
    ViewBag.Title = "DisplayElements";
    Layout = "~/Views/Shared/_LayoutPage1.cshtml";
}

<h2>DisplayElements</h2>

@{ 
    foreach (var item in Model.dictionaryOfElements)
    {
        <p>
            Atomic Number:
            Symbol:
            Name:
            Weight:
        </p>

    }
}


I want to be able to loop through the dictionary and print all the elements.

    <p>
                Atomic Number:@item.AtomicNumber
                Symbol: @item.Symbol
                Name: @item.Name
                Weight: @item.Weight
    </p>

以下循环对我不起作用。

foreach(KeyValuePair<int, Elements> elementKeyValuePair in ictionaryOfElements)
        {

        }

另外,如果我只想按字典键搜索,如何显示所有值? 例如,我希望能够执行以下操作:

Elements element = dictionaryOfElements[4];

最后,我将添加元素周期表的所有118个元素。 这是最好的方法吗?

首先,您应该从控制器返回ElementsOfPeriodicTable对象:

 public ActionResult DisplayElements()
{          
    dictionaryOfElements.Add(Hydrogen.AtomicNumber, Hydrogen);
    dictionaryOfElements.Add(Helium.AtomicNumber, Helium);
    dictionaryOfElements.Add(Lithium.AtomicNumber, Lithium);
    dictionaryOfElements.Add(Beryllium.AtomicNumber, Beryllium);

    return View(new ElementsOfPeriodicTable{
                                          dictionaryOfElements=dictionaryOfElements
                                           }
             );
}

如果您这样做,我认为foreach循环会起作用:

foreach(KeyValuePair<int, Elements> elementKeyValuePair in ictionaryOfElements)
        {

        }

foreach (KeyValuePair<int, Elements> item in dictionaryOfElements)将迭代foreach (KeyValuePair<int, Elements> item in dictionaryOfElements)每个KeyValuePair ,因此,在HTML中,您可以执行以下操作:

    @foreach(KeyValuePair<int, Elements> item in dictionaryOfElements)
    {
         <p>
            Atomic Number:item.AtomicNumber
            Symbol: item.Symbol
            Name: item.Name
            Weight: item.Weight
         </p>
    }

为了检索特定的项目,可以使用一个基于键的TryGetValue,它会尝试根据一个键检索一个值。 根据密钥是否存在,该方法将返回true或false。 除非您的键为空,否则没有异常要处理。

在HTML中这样说,您可以执行以下操作:

    @{
        Elements item = new Elements();
        if (dictionaryOfElements.TryGetValue(4, out item))
        {
            <p>
                item.AtomicNumber
                item.Symbol
                item.Name
                item.Weight
            </p>
        }
    };

请注意,您可能需要在HTML的开头指定@using元素。

最后回答您的问题,是的,这似乎是一个好方法。 我唯一的建议是创建一个与Model Elements具有相同结构的ViewModel类ElementViewModel。

ViewModel将用于将数据从控制器传递到视图。 简而言之,模型可以定义您的数据结构,如果可以的话-它可以反映数据库或数据的表示方式。

另一方面,viewModel是您将传递给View的数据结构。

在您的示例中-也许-这样做没有任何好处,但是我认为学习正确的结构将帮助您学习C Sharp。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM