Convert enum to string

157 views
Skip to first unread message

swetha reddy

unread,
Feb 4, 2019, 4:44:40 PM2/4/19
to KnockoutJS

unable to display the value . Should be " home " but displaying 1 

ChucK Norris

unread,
Feb 5, 2019, 9:32:10 AM2/5/19
to KnockoutJS
coul you provide some code to understand your problem ? 
thx 

swetha reddy

unread,
Feb 5, 2019, 9:36:27 AM2/5/19
to KnockoutJS

public enum OwnershipType
{
Buying,
Owns,
Renting,
[AS400Value("Other")]
Boards,
[Display(Name = "Live with Relative"), AS400Value("Other")]
LiveWithRelative,
Other
}

EDIT sscreen where I select the value in teh dropdown

<div class="form-group">
<label asp-for="OwnershipType" style="float:left;margin-right:5px;"></label>
<div class="input-group col-md-6">

<select class="form-control" asp-for="OwnershipType" 
asp-items="@Html.GetEnumSelectList<Ucfs.LoanPortal.Data.Entities.OwnershipType>
()" data-bind="value :ownershipType">
<option>Select</option>
</select>
<span asp-validation-for="OwnershipType"></span>
</div>
</div>

THIS IS THE DISPLAY CODE : AND RIGHT NOW IF I SELECT BUYING : IT RETURNS 0 INSTEAD OF "BUYING" 

<div class="col-md-6">
<div class="form-row">
<label class="col-md-5 col-form-label" asp-for="OwnershipType">Type Of Housing</label>
<div class="col-md">
<span class="form-control-plaintext" data-bind="text: ownershipType"></span>
</div>
</div>
</div>

PLEASE HELP ME 

ChucK Norris

unread,
Feb 5, 2019, 10:00:04 AM2/5/19
to KnockoutJS
if it's return 0 for buying, 1 for Owns, 2 for Renting etc ... it's ok because you can find the associated Enum in javascript, 
you mix c# and knockout here so i think it's hard doing what you want without little trick, 

An easier way, it's to load your enum in the javascript on a format like : Id, Label  then use the option binding https://knockoutjs.com/documentation/options-binding.html

swetha reddy

unread,
Feb 5, 2019, 10:35:00 AM2/5/19
to KnockoutJS
you mix c# and knockout here so i think it's hard doing what you want without little trick ---what is that little trick ???

ChucK Norris

unread,
Feb 5, 2019, 10:47:06 AM2/5/19
to KnockoutJS
this => if it's return 0 for buying, 1 for Owns, 2 for Renting etc ... it's ok because you can find the associated Enum in javascript, 
or this => An easier way, it's to load your enum in the javascript on a format like : Id, Label  then use the option binding https://knockoutjs.com/documentation/options-binding.html

SLG

unread,
Feb 5, 2019, 2:38:20 PM2/5/19
to KnockoutJS
In this scenario, the options of the select will be generated on server side.

So when the page is loaded. The select control is already filled with option.

In this case the default Knockout select binding might not work. It expects an 'Options' binding.

Maybe you can use the event binding to handle the change event?

Or 

You can try is to serialize the serverside enum to javascript array then use select + options binding. 
Reply all
Reply to author
Forward
0 new messages