Reputation: 1681
The below correctly renders a label and textbox for each item added to the list instantiated in the controller.
What I need is for the label to display the value of "FavouriteThingID" (i.e 1,2,3,4,5) rather than the literal "FavouriteThingID".
Any ideas?
ViewModel
public class FavouriteThingViewModel
{
public int FavouriteThingID { get; set; }
public string FavouriteThingName { get; set; }
}
Editor Template
@model _5fave.Models.FavouriteThingViewModel
<div class="control-group">
@Html.LabelFor(m => m.FavouriteThingID, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.FavouriteThingName)
@Html.ValidationMessageFor(m => m.FavouriteThingName, null, new { @class = "help-inline" })
</div>
</div>
Controller
var favouriteThings = new List<FavouriteThingViewModel>();
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 1, FavouriteThingName = "Raindrops on roses" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 2, FavouriteThingName = "Whiskers on kittens" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 3, FavouriteThingName = "Bright copper kettles" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 4, FavouriteThingName = "Warm woolen mittens" });
favouriteThings.Add(new FavouriteThingViewModel { FavouriteThingID = 5, FavouriteThingName = "Brown paper packages" });
viewModel.FavouriteThings = favouriteThings;
Upvotes: 4
Views: 11646
Reputation: 15609
Try
@Html.LabelFor(m => m.FavouriteThingName, Model.FavouriteThingID);
As the label is for the textbox for property FavouriteThingName
.
From comments below.
If your property is an int
convert to string
.
@Html.LabelFor(m => m.FavouriteThingName, Model.FavouriteThingID.ToString());
Upvotes: 6
Reputation: 1681
This is working:
@Html.LabelFor(m => m.FavouriteThingName, Model.FavouriteThingID.ToString());
Upvotes: 1
Reputation: 3215
The best way is to set the Display attribute for that prperty
public class FavouriteThingViewModel
{
public int FavouriteThingID { get; set; }
public string FavouriteThingName { get; set; }
}
In your cshtml file
@Html.DisplayFor(m => m.FavouriteThingID)
This will display the values of the property. hope this helps.
Upvotes: 3