按值更改 html img 来自 json 数据,该数据获取带有一些可为空字段的数据库值

发布于 2024-11-30 22:39:51 字数 1291 浏览 0 评论 0原文

我有一个包含以下字段的数据库表:

产品 ID |设计ID |材料 ID |颜色_id | picture1(图片名称字段)..

design_idmaterial_idcolor_id 是数据库中可为空的字段..

我正在使用 linqtosql< /代码> 为它。

我有 id 字段的下拉列表。下拉菜单的顺序如下:

产品>设计>材料>颜色

这是一条牢不可破的路线。 (是的,我想出了如何做到这一点)

当我选择第一个下拉项(产品列表项)时,我希望更改我的图片。然后,当选择进一步的下拉项目时,我希望它再次改变。 (例如:选择产品后,选择设计时,我希望它找到与产品和设计 ID 匹配的图片(可以在数据库中找到)。

因此,如果 product=1, < code>design=0、material=0color=0 将显示这种情况的图片,如果 product=3 >、设计=6材料=2 color=9 将显示这种不同情况的图片

编辑:我现在可以更改图像,但无法仅在屏幕上显示链接。

这是 。新代码:

jquery

    $.getJSON('@Url.Content("~/Admin/GetPictures/")', { productId: prod, designId: des, matsid: mats, colid: col }, function (data) {
        $.each(data, function (i, c) {
            /*$("img#res").attr("src", "../../Pictures/" + c.Text).attr("alt", "../../Pictures/" + c.Text).attr("width", "100%");*/
              $("img#res").attr({ 'src': '../../Pictures/' + c.Text, 'alt': '../../Pictures/' + c.Text, 'width': '100%' });
        })
    })

i have a database table that holds the following fields:

product_id | design_id | material_id | color_id | picture1 (picture name field)..

design_id, material_id and color_id are nullable fields in the database..

I am using linqtosql for it.

I have dropdownlists for the id fields. The dropdowns are in order as so:

Product>Design>Material>Color

This is an unbreakable line. (yeah, I figured how to do this)

When I choose the first dropdown item (Product list item) I want my picture to change. Then when choosing further dropdown items I want it to change again. (for example: after product is chosen when a design is chosen I want it to find the picture which matches product and design id (which can be found in the database).

So if product=1, design=0, material=0 and color=0 the picture for this situation will be displayed and if product=3, design=6, material=2 and color=9 the picture for this different situation will be displayed.

EDIT : I can now change the image but cant display on the screen only the link comes..

Here is new code:

jquery

    $.getJSON('@Url.Content("~/Admin/GetPictures/")', { productId: prod, designId: des, matsid: mats, colid: col }, function (data) {
        $.each(data, function (i, c) {
            /*$("img#res").attr("src", "../../Pictures/" + c.Text).attr("alt", "../../Pictures/" + c.Text).attr("width", "100%");*/
              $("img#res").attr({ 'src': '../../Pictures/' + c.Text, 'alt': '../../Pictures/' + c.Text, 'width': '100%' });
        })
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2024-12-07 22:39:51

找到了解决此问题的方法:

控制器

public JsonResult GetPictures(int productId, int? designId, int? matsid, int? colid)
{
    int? d = 0;
    int? m = 0;
    int? c = 0;
    if (designId == null) { d = 0; } else if (designId != null) { d = designId; };
    if (matsid == null) { m = 0; } else if (matsid != null) { m = matsid;};
    if (colid == null) { c = 0; } else if (colid != null) { c = colid; };
    IEnumerable<SelectListItem> PictureItems = db.Pictures.Where(x => x.product_id == productId & x.design_id == d & x.material_id == m & x.color_id == c).AsEnumerable().Select(x => new SelectListItem()
    {
        Text = x.picture1,
        Value = x.id.ToString().TrimEnd() 
    });
    SelectList data = new SelectList(PictureItems, "Value", "Text");
    return Json(data, JsonRequestBehavior.AllowGet);
}

视图

$.getJSON('@Url.Content("~/Admin/GetPictures/")', { productId: prod, designId: des, matsid: mats, colid: col }, function (data) {
    $.each(data, function (i, c) {
        $("img#res").attr("src", "../../Pictures/" + c.Text).attr("alt", "../../Pictures/" + c.Text);
    })
})

Found a solution for this problem :

controller

public JsonResult GetPictures(int productId, int? designId, int? matsid, int? colid)
{
    int? d = 0;
    int? m = 0;
    int? c = 0;
    if (designId == null) { d = 0; } else if (designId != null) { d = designId; };
    if (matsid == null) { m = 0; } else if (matsid != null) { m = matsid;};
    if (colid == null) { c = 0; } else if (colid != null) { c = colid; };
    IEnumerable<SelectListItem> PictureItems = db.Pictures.Where(x => x.product_id == productId & x.design_id == d & x.material_id == m & x.color_id == c).AsEnumerable().Select(x => new SelectListItem()
    {
        Text = x.picture1,
        Value = x.id.ToString().TrimEnd() 
    });
    SelectList data = new SelectList(PictureItems, "Value", "Text");
    return Json(data, JsonRequestBehavior.AllowGet);
}

view

$.getJSON('@Url.Content("~/Admin/GetPictures/")', { productId: prod, designId: des, matsid: mats, colid: col }, function (data) {
    $.each(data, function (i, c) {
        $("img#res").attr("src", "../../Pictures/" + c.Text).attr("alt", "../../Pictures/" + c.Text);
    })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文