0

I need to generate labels in view for each item in an array which is in the model.

This is what I have so far.

Reference.cs

public class Reference
{
    private string[] itemList = { "Shirt", "T-shirt", "Denim" };


    public string[] Item {
        get{
            return itemList;
        }

        set {
            itemList = value;
        }
    }
    public int ShirtId{get;set;}
    public int TShirtId { get; set; }
    public int DenimId { get; set; }
}

index.cshtml

@model WebApplication1.Models.Reference

@{
    ViewBag.Title = "Index";
}

<h2>Update Your References Here</h2>
<br>
<p> Please type in the reference code of the item in the corresponding text box</p>
<section id="reference-update">
   @using (Html.BeginForm()) { 
        <fieldset>
            <legend> Reference Items</legend>

            <div class="form-horizontal">


                <div class="item">
                    @Html.LabelFor(model=>model.Item[0])
                </div>
                <div class="input-lg">
                    @Html.TextBoxFor(model => model.ShirtId)
                </div>
            </div>
        </fieldset>
   }
</section>

But I do not get a label named "Shirt" in the view(A label Named "Item[0]" appears). How do I fix this? Note that I am a beginner.

EDIT: Controller code

public class ReferenceController : Controller
{
    // GET: Reference
    [Authorize]
    public ActionResult Index()
    {
        return View("Index");
    }
}
3
  • Does this work? model=>model.Item.itemList[0] Commented Jun 29, 2015 at 7:26
  • nope. one reason is it's access modifier. but even if I change it to public, it doesn't work Commented Jun 29, 2015 at 7:28
  • It will never work as your view expect a model object that you do not pass to view from controller. Commented Jun 29, 2015 at 7:57

3 Answers 3

2

It won't work for @Html.LabelFor() since as you look at helper's signature:

public static MvcHtmlString LabelFor<TModel, TValue>(
   this HtmlHelper<TModel> html,
   Expression<Func<TModel, TValue>> expression
)

you need an expression which identifies property to display. Array item is not a property which can be displayed.

You can change your HTML to:

@model WebApplication1.Models.Reference

@{
   ViewBag.Title = "Index";
}

<h2>Update Your References Here</h2>
<br>
<p> Please type in the reference code of the item in the corresponding text box</p>
   <section id="reference-update">
    @using (Html.BeginForm()) { 
    <fieldset>
        <legend> Reference Items</legend>

        <div class="form-horizontal">
            @foreach(var item in Model.Item)
            {
               <div class="item">
                  <label>@item</label>
               </div>
            }
            <div class="input-lg">
                @Html.TextBoxFor(model => model.ShirtId)
            </div>
        </div>
    </fieldset>
  }

If you want just to generate label for each item in your model.

EDIT:

Change your controller code to:

public class ReferenceController : Controller
{
    // GET: Reference
    [Authorize]
    public ActionResult Index()
    {
        var model = new WebApplication1.Models.Reference();
        return View("Index", model);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting a NullReference Exception
@DesirePRG - Have you initialized your model and passed it as View() parameter in your controller?
I have updated the question with the controller code. Have I done anything wrong in it?
@DesirePRG - great! Feel free to accept and upvote my answer if you wish.
1

I would have structured your model differently that would simplify this scenario:

public class ItemModel
{
    public int ItemId { get; set; }
    public string Description { get; set; }
    public string Label { get; set; }
}

public class ItemListViewModel
{
    public IList<ItemModel> ItemList { get; set; }
}

Now your view can operate on an object of type ItemListViewModel and iterate through items stored in it.

Advantage over your current approach:

  1. You can easily add more items when need i.e. Shorts (id and type).
  2. Simplified view code.
  3. You can even use customized label for each type.

Comments

0

use @Html.Label(Model.Item[0])

<div class="item">
                    @Html.Label(Model.Item[0])

                </div>

4 Comments

I am getting a NullReference Exception
Please explain your answer.
Well if you want to access value of the item to display it as label then as mentioned in other answers LabelFor signature is not correct. need to use .Label
let me know if you would need more details to explain. Agree that my answer is does not have detailed explanation to educate OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.