0

I am trying to write a simple web application which displays certain details dependent on which ID is picked from a select list.

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

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:

htmlhelper does not contain a definition for DisplayNameFor and no extension method DisplayNameFor accepting a first argument of type 'HTMLHelper' could be found. Are you missing an assembley or reference?

I read that I should have '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.

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?

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.

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?

Thanks.

  • did you try scaffolding?, your views – Jephren Naicker Aug 28 '17 at 11:18
  • Thanks for the reply, I have previously use scaffolding but I was trying to do it manually this time to get a better understanding of how everything works. I would ideally like to be able to do what scaffolding/EF does but manually as I am trying to learn and understand what is going on when the code is written and what happens when I run it. –  Aug 28 '17 at 11:45
  • try reading this https://stackoverflow.com/questions/9465376/when-should-i-use-html-displayfor-in-mvc , and this https://stackoverflow.com/questions/6365633/what-is-the-html-displayfor-syntax-for – Jephren Naicker Aug 28 '17 at 11:51

1 Answers1

0

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);
        }
Libin C Jacob
  • 1,108
  • 12
  • 34