0

I'm new in MVC, I have two Call Ajax, want to use result of the first call in a second call, but the results comes back two times. second call ajax shows image of one of the results in first call ajax. this is my action in the controller:

public ActionResult GetMaterialsPic(int id)
{
    ViewBag.Pic = db.MaterialPictures.FirstOrDefault(p => p.EntityId == id)?.Id;
    var  picFiles = db.MaterialPictures.Where(p => p.EntityId == id).Select(p => new {p.EntityId,p.Id,p.SystemFile}).FirstOrDefault();

    if (picFiles?.SystemFile != null)
        ViewBag.PicFile = Convert.ToBase64String(picFiles.SystemFile);

    return Json(picFiles,JsonRequestBehavior.AllowGet);
}

this is my call ajax:

  $.ajax({
            url: 'GetMaterials',
            type: "GET",
            dataType: "JSON",
            data: { id: radioselect },
            success: function (materials) {
                $("#MaterialId").html("");
                $.each(materials, function (i, value) {

                    var Id=value.MaterialId;
                    $.ajax({
                        url: 'GetMaterialsPic',
                        type: "GET",
                        dataType: "JSON",
                        data: { id: Id },
                        success: function (picFiles, status, jqXhr) {
                            var data = '@(ViewBag.PicFile)';
                            $("#MaterialId").append(
                $('<label class="radio-inline"><input type="radio"  name="MaterialsId" value="' + value.Name + '" style="position: inherit;margin-left: 20px;" />' + value.Name + ' </label><img src="data:image/png;base64,' + data + '" style="display: inline;">'));

                        },
                        error: function (jqXhr, status, errorMsg) {
                            $("#MaterialId").append(
                                             $('<label class="radio-inline"><input type="radio"  name="MaterialsId" value="' + value.Name + '" style="position: inherit;margin-left: 20px;" />' + value.Name + ' </label> <img class="img-null" alt="عکس" style="display: inline;">'));
                        }
                    });
                });
            }
        });
Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
R.Mrz
  • 1
  • 6

0 Answers0