So I have this piece of code in my displaytemplate:
@Html.DropDownListFor(blah => blah.InputtedData, ddv)
and I get an actual dropdownlist. I’ve tried this piece of code:
@Html.DisplayFor(blah => blah.InputtedData, ddv)
And I get 1 (the number one), the selectedvalue. But I want to display Yes, the selectedtext. So how do I display the selectedtext in a dropdownlist in a displaytemplate and not the selectedvalue?
Per request:
namespace TESTMVC.ViewModels
{
public class CtrlInputDataModel
{
public CtrlTypeModel RowCtrl { get; set; }
public long InputtedDataID { get; set; }
public string InputtedData { get; set; }
public DateTime InputtedDate { get; set; }
public CtrlInputDataModel()
{
}
public CtrlInputDataModel (CtrlTypeModel newRowCtrl, long newInputtedDataID, string newInputtedData, DateTime newInputtedDate)
{
RowCtrl = newRowCtrl;
InputtedDataID = newInputtedDataID;
InputtedData = newInputtedData;
InputtedDate = newInputtedDate;
}
}
}
The ViewModel that ddv is based on:
namespace TESTMVC.ViewModels
{
public class DefaultValueModel
{
public string Label { get; set; }
public string Value { get; set; }
public DefaultValueModel()
{
}
public DefaultValueModel(string newLabel, string newValue)
{
Label = newLabel;
Value = newValue;
}
}
}
You cannot do it with the built in
Html.DisplayFor. However you can create a custom displaytemplate e.g./Views/Shared/DisplayTemplates/DropdownListTest.cshtmlwhere you manually select the text from the dropdownlist values based on your property:Then you can use it with the following (little bit complex) syntax:
But if don’t plan to reuse this functionality just inline your the logic and use this instead of the
Html.DisplayFor: