0

I am learning MVC4. I could display records in a tabular format using foreach.

Now, I need to display theDescription of (only) first Topic object in a label. I need to do it without a foreach. How can we do it?

VIEW

@model MvcSampleApplication.Models.LabelDisplay

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{


    foreach (var item in Model.Topics.Select((model, index) => new { index, model }))
    {
    <div>@(item.index) --- @item.model.Description---- @item.model.Code</div> <div></div>
    }
}

Controller Action

public ActionResult Index()
        {
            LabelDisplay model = new LabelDisplay();

            Topic t = new Topic();
            t.Description = "Computer";
            t.Code=101;

            Topic t3 = new Topic();
            t3.Description = "Electrical";
            t3.Code = 102;


            model.Topics = new List<Topic>();
            model.Topics.Add(t);
            model.Topics.Add(t3);

            return View(model);
        }

Model

namespace MvcSampleApplication.Models
{
    public class LabelDisplay
    {
        public List<Topic> Topics;
    }

    public class Topic
    {
        public string Description { get; set; }
        public int Code { get; set; }
    }
}

REFERENCE

  1. Iterate through collection and print Index and Item in Razor
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • don't do your LINQ in view. Create a view model for your view and send data in that – Shyju Jul 31 '13 at 16:31
  • 1
    @Shyju: performing a LINQ *query* (something that will actually cause a database query to be issued) in view would be wrong, but LINQ, in general, is not necessarily "wrong" to have in your view. In particular, the OP here is following a common pattern to get a index value inside a foreach, which is perfectly fine. – Chris Pratt Jul 31 '13 at 17:18

1 Answers1

3

I need to display theDescription of (only) first Topic object in a label

Unless I totally misunderstood you, selecting the first item (only) in your view would look something like:

@if (Model.Topics.Any())
{
    @Html.DisplayFor(x => x.Topics.First().Description)
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • 1
    I'm reasonably sure that won't work using `LabelFor` -- you'll get a runtime error. However, the OP can use the `Model.Topics.First().Description` bit with just a static label tag easily enough. – Chris Pratt Jul 31 '13 at 17:14
  • 1
    @ChrisPratt Wow, actually ... this is a bit embarassing, you see it won't throw an exception, but it's actually adding a `label` element to the page ... Yeah, it's been a long day ... – Dimitar Dimitrov Jul 31 '13 at 17:38
  • @DimitarDimitrov The LableFor is not in your updated answer. Don't we need it? – LCJ Aug 01 '13 at 03:20
  • 1
    @Lijo The `LabelFor` helper will generate an html element `` rather than just output the description itself. That said, it has an overload that accepts the text contents as a parameter. However an Html label element is usually used on forms that have corresponding textboxes/inputs for it. Not sure that is the case for you. – Dimitar Dimitrov Aug 01 '13 at 03:34
  • 1
    @Lijo This might help as well: http://stackoverflow.com/questions/7636502/why-use-label – Dimitar Dimitrov Aug 01 '13 at 03:40
  • 1
    @Lijo IMHO (and what I would do is simply use `@Html.DisplayFor`, I've edited the answer. – Dimitar Dimitrov Aug 01 '13 at 04:19