I have save store image name in database and image file in local folder I have used to bind the image
<img width="16px" height="16px" data-bind="attr:{src: PhotoName}" />
in html it's show
<img src="http://sitename.com/Controller/action/imagename.extension"/>
but I need
<img src="http://sitename.com/imagefolder/imagename.extension"/>
any idea how can i fix this?? Thanks in advance.
Essentially a binding or a data binding is a way to link your ViewModels to your Views(templates) and vice versa. KnockoutJS uses two-way data binding, which means changes to your ViewModel influence the View and changes to your View can influence the ViewModel.
The $data variable is a built-in variable used to refer to the current object being bound. In the example this is the one of the elements in the viewModel.
To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.
Your issue has nothing to with kncokout.js. If your PhotoName
only contains the imagename.extension
you need to build your image path by hand in order to display the images correctly.
So you need to create the correct path either in the binding directly:
<img data-bind="attr:{ src: '/imagefolder/' + PhotoName }" />
Note if your PhotoName
is a ko.observable
then you need to write src: '/imagefolder/' + PhotoName()
.
Or move this logic inside your viewmodel e.g. creating a computed property which does the link building or when you create your viewmodel assign the correct url to PhotoName
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With