简体   繁体   English

在从控制器返回的视图中显示数据

[英]Displaying data in a view returned from a controller

I am trying to write a simple web application which displays certain details dependent on which ID is picked from a select list. 我正在尝试编写一个简单的Web应用程序,该应用程序显示某些细节,具体取决于从选择列表中选择的ID。

I can get the data from the database using the following: 我可以使用以下方法从数据库中获取数据:

Class: 类:

namespace InterviewTest.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;
[Table("Widget")]

        public partial class Widget
        {
            public int WidgetID { get; set; }

            [Required]
            [StringLength(50)]
            public string WidgetName { get; set; }

            [Required]
            public string WidgetDescription { get; set; }

            public int WidgetColourID { get; set; }
        }

}

Controller: 控制器:

using System.Linq;
using System.Web.Mvc;
using InterviewTest.Models;

namespace InterviewTest.Controllers
{
    public class HomeController : Controller
    {
        WidgetConn db = new WidgetConn();

        public virtual ActionResult Index(Widget widget)
        {


            var widgets =
                (from w in db.Widgets
                 where w.WidgetID == 1
                 select new
                    {
                     WidgetName = w.WidgetName,
                     WidgetDescription = w.WidgetDescription,
                     WidgetColourID = w.WidgetColourID
                    }).ToList();

            var data = widgets[0];

            return View(data);
        }
    }
}

The data returned in 'data' is returned as: 在“数据”中返回的数据返回为:

WidgetID 1 WidgetDescription Test WidgetColourID 1 WidgetID 1 WidgetDescription测试WidgetColourID 1

Test View: 测试视图:

@model InterviewTest.Models.Widget
@{
    ViewBag.Title = "test";
}

<h2>test</h2>

<div>
    <h4>Widget</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.WidgetName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.WidgetName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.WidgetDescription)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.WidgetDescription)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.WidgetColourID)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.WidgetColourID)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.WidgetID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

When I try to us @Html.DisplayNameFor(model => model.WidgetName) to display the data returned I get the error: 当我尝试使用@ Html.DisplayNameFor(model => model.WidgetName)显示返回的数据时,出现错误:

htmlhelper does not contain a definition for DisplayNameFor and no extension method DisplayNameFor accepting a first argument of type 'HTMLHelper' could be found. htmlhelper不包含DisplayNameFor的定义,找不到可以接受类型为'HTMLHelper'的第一个参数的扩展方法DisplayNameFor。 Are you missing an assembley or reference? 您是否缺少装配或参考?

I read that I should have 'using System.Web.Mvc.HtmlHelper;' 我读到我应该有“ using System.Web.Mvc.HtmlHelper;”。 however this appears to be a namespace so cannot be used in this way. 但是,这似乎是一个名称空间,因此不能以这种方式使用。 Changing my namespace means I can no longer access this class. 更改名称空间意味着我将无法再访问此类。

I also read that System.Web.Mvc.Html should containt the 'HtmlHelper' but using this has not resolved my issue. 我还读到System.Web.Mvc.Html应该包含'HtmlHelper',但是使用它不能解决我的问题。

My questions are: 我的问题是:

1: Should I be using @Html.DisplayNameFor(model => model.WidgetName) with a .cshtml file or is there another way to access the data? 1:我应该将.Html.DisplayNameFor(model => model.WidgetName)与.cshtml文件一起使用,还是有另一种访问数据的方法?

2: If the way I am trying to access the data is correct, how/where do I add the namespace so I can access the HtmlHelper namespace. 2:如果我尝试访问数据的方式正确,那么如何/在何处添加名称空间,以便可以访问HtmlHelper名称空间。

3: If the the way I am trying to access the data is NOT correct for a .cshtml file please could someone point me to some documentation I could read? 3:如果对于.cshtml文件,我尝试访问数据的方式不正确,请有人指出我可以阅读的一些文档吗?

Thanks. 谢谢。

I think it is because you are selecting the data as an unknown type. 我认为这是因为您正在选择数据作为未知类型。 Instead of unknown, specify its type. 而不是未知,请指定其类型。 So please replace your code as shown below. 因此,请如下所示替换您的代码。

 public virtual ActionResult Index(Widget widget)
        {
            IEnumerable<Widget> widgets =
                (from w in db.Widgets
                 where w.WidgetID == 1
                 select new Widget 
                    {
                     WidgetName = w.WidgetName,
                     WidgetDescription = w.WidgetDescription,
                     WidgetColourID = w.WidgetColourID
                    }).ToList();

            var data = (widgets!=null && widgets.length>0)? widgets[0]: null;

            return View(data);
        }

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

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