lundi 29 juin 2015

How to access array values from view in asp.net mvc5?

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.

Aucun commentaire:

Enregistrer un commentaire