1

我尝试使用组合选择更改加载图像:

通常我可以通过以下方式加载图像:

剃刀视图:

<img id="imgPhotoIcon" src="@Url.Action("GetPhoto", "Contractor", new { id = Model.ContactPersonnelID })" alt="Photo" style="width:150px; height:150px" />

控制器部分:

public Image GetPhoto(int id)
        {
            ContactPersonnel oContactPersonnel = new ContactPersonnel();
            oContactPersonnel = oContactPersonnel.GetWithImage(id, (Guid)Session[SessionInfo.wcfSessionID]);
            if (oContactPersonnel.Photo != null)
            {
                MemoryStream m = new MemoryStream(oContactPersonnel.Photo);
                System.Drawing.Image img = System.Drawing.Image.FromStream(m);
                img.Save(Response.OutputStream, ImageFormat.Jpeg);
                return img;
            }
            else
            {
                return null;
            }
        }

现在我想根据所选项目加载带有组合选择更改的图像,任何人都可以帮助我

4

1 回答 1

1

我认为这里的基本问题是您需要将图像的 url 作为图像源,因此您需要使用 json 返回图像 url。以下是 ajax 方法,该方法将被称为组合框的 onchange。Id 是您要传递给的输入方法。或者您可以将字节数组的base64string返回给ajax成功

$.ajax({
                            cache:true,
                            type: "POST",
                            url: "@(Url.Action("GetPhoto", "Contractor"))",
                            data: "id=" + id,
                            success: function (data) {

  $('#imgPhotoIcon').attr('src', "data:image/jpg;base64," + data);

                                                   }
        });

以下是控制器动作

public Actionresult GetPhoto(int id)
        {
       //logic to get picture url goes here 
       // string picture=GetPictureUrl(id);

      byte[] imageByteArray = images;//Return byte array here
return Json(new { base64imgage =  Convert.ToBase64String(imageByteArray) }
                , JsonRequestBehavior.AllowGet);
        }

希望能帮助到你

于 2013-10-10T06:59:37.623 回答