How to pass ViewData from Controller to my JS

让人想犯罪 __ 提交于 2019-12-08 15:25:33

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(@Filter.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);
    };
Mahavirsinh Padhiyar

Use below:

<script type="text/javascript">
    var test = <%= serializer.Serialize(ViewData["Filter"]) %>;
</script>

Or

May be this one will help you out.

Solution 1

Solution 2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!