Reputation: 349
I have the Drop down list which is populated from a GetJSON call as shown below
VIEW
@{
var NoticeFilter =(X.Models.Y.Z.NoticesEntity) ViewData["NoticeFilter"];
}
<div class="form-group">
<label>Field Office</label>
<select data-bind="options: FieldOffice, value: selectedFieldOffice, optionsCaption:'Choose...', optionsValue:'FieldOfficeID', optionsText:'Name'">
@if(NoticeFilter!=null)
{
<option value="@NoticeFilter.FieldOfficeID" selected></option>
}
</select>
</div>
When I direct to this page and send data into the NoticeFilter I want the value in drop-down pre-selected with the value in Noticefilter among the other values .How do I achieve this
I was wondering if there is way in razor HTML where I can set a default value to Drop-Down after the data-binding from KO JS
Upvotes: 1
Views: 1396
Reputation: 349
WORKING After few attempts I found this as one way to access the ViewData or ViewBag in my JS
I used the KO JS optionsAfterRender as below
VIEW
<select data-bind="options: SubType, value: selectedSubType, optionsValue:'SubTypeID', optionsText:'SubTypeDescription',optionsAfterRender:function(){setOptionST(@Noticefilter .SubTypeID);}"></select>
JS
In the view model
self.setOptionST = function (x) {
//here we can do whatever is intended to in my case to set the initial value in dropdown
self.selectedSubType(x);
};
Upvotes: 0
Reputation: 900
Have you tried this?
<script>
var str = @Html.Raw(Json.Encode(ViewData["Text"]));
</script>
Upvotes: 1