[英]Display an images using data-bind knockout.js
I am working on a table for a web application using MVC and knockout.js. 我正在为使用MVC和kickout.js的Web应用程序制作表格。 I have experience doing web development but this is my first time using knockout.
我有进行Web开发的经验,但这是我第一次使用淘汰赛。 I currently have a table with 3 columns.
我目前有一张3列的表格。 The 2nd and 3rd are populated using a knockout function that displays the data.
使用显示数据的基因剔除功能填充第二个和第三个。 I tried to set up the first column the same way except using an image instead of text.
我尝试以相同的方式设置第一列,除了使用图像而不是文本。 I keep getting a broken image icon and an error in the brower's console.
我在浏览器的控制台中不断出现图像图标损坏和错误。
The error the browser is giving me: GET http:// hostinfo /Sponsor/~PracticeAppImagesGWC.png 404 (Not Found) 浏览器给我的错误:GET http:// hostinfo /Sponsor/~PracticeAppImagesGWC.png 404(未找到)
This is my table: 这是我的桌子:
<table id="sponsorTable">
<thead><tr>
<th></th><th id="sponsor">Sponsor</th><th id="description">Description</th>
</tr></thead>
<!-- Todo: Generate table body -->
<tbody data-bind="foreach: Sponsors">
<tr>
<td><img data-bind="attr: {src: Image}" /></td>
<td class="sTableInfo" data-bind="text: Name"></td>
<td class="sTableInfo" data-bind="text: Description"></td>
</tr>
</tbody>
</table>
This is my function for filling the table. 这是我填写表格的功能。 (columns 2 and 3 fill properly)
(第2列和第3列正确填充)
function pageModel() {
var self = this;
self.Sponsors = ko.observableArray([]);
}
function Sponsor( _image, _name, _descrip)
{
var self = this;
self.Image = ko.observable(_image);
self.Name = ko.observable(_name);
self.Description = ko.observable(_descrip);
}
var viewModel = new pageModel();
viewModel.Sponsors().push(new Sponsor("~/Images/GWC.png", "name1", "info1"));
viewModel.Sponsors().push(new Sponsor("second image would go here", "name2", "info2"));
$(function () {
ko.applyBindings(viewModel);
})
I think something is escaping the slashes in the img source link, but I'm not sure and I can't figure out why. 我认为img源链接中的斜线已经转义了,但是我不确定,我也不知道为什么。
UPDATE: my compiler is telling me this for the image tag: "Validation (HTML5): Element 'img' is missing required attribute 'src'." 更新:我的编译器告诉我这个图像标记:“验证(HTML5):元素'img'缺少必需的属性'src'。”
Pass just image name from viewmodel, and static folder path can be append like this. 仅从viewmodel中传递图像名称,并且可以这样添加静态文件夹路径。
// in viewmodel
viewModel.Sponsors().push(new Sponsor("GWC.png", "name1", "info1"));
// inside table
<td><img data-bind="attr: {src:'/Image/'+ Image}" /></td>
Hope this helps... 希望这可以帮助...
The push
must be on the observableArray itself, you need to remove the parenthesis: push
必须在observableArray本身上,您需要删除括号:
var viewModel = new pageModel();
viewModel.Sponsors.push(new Sponsor("~/Images/GWC.png", "name1", "info1"));
viewModel.Sponsors.push(new Sponsor("second image would go here", "name2", "info2"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.