Muhammad Naveed Muhammad Naveed - 3 months ago 11
ASP.NET (C#) Question

MVC EF6 Display images related to User

Related to my Previous Question

how we can display Images related to the User....who stored them.

we want to Display Images related to the PatientID

My Index Controller is

// GET: Imagings
public ActionResult Index(int id)
{
co.Values.Add("PID", id.ToString());
co.Expires.AddMinutes(2.0);
Response.SetCookie(co);
pId = id;

var res= db.Patients.Where(x => x.PatientID==id).ToList();
foreach(var r in res)
{
ViewBag.PName = r.First_Name;
}
var imagings = db.Imagings.Include(i => i.ImageType).Include(i => i.Patient);
return View(imagings.ToList());
}


and my View is

@model IEnumerable<iSmileFinal.Models.Imaging>
@if (Model != null)
{
<div class="adsm-sec">
@foreach (var vad in Model)
{
<div id="img">
<div class="adsm-image-wraper">

<img src="@Url.Content("~")UserImages/@vad.ImageUrl" alt="no photo" class="adsm-image"/>
</div>
</div>


@Html.DisplayFor(modelItem => vad.PatientID)

@Html.DisplayFor(modelItem => vad.ImageType)

@Html.DisplayFor(modelItem => vad.Comments)
}
</div>


}

Thanks

Answer

Use LINQ where method to filter images for only one patient

public ActionResult Index(int id)
{
   var imageList = db.Imagings.Where(a=>a.PatientID==id)
                          .Include(i => i.ImageType)
                          .Include(i => i.Patient).ToList();
   return View(imageList);
}

Assuming your Imaging entity has a PatientID property and you are storing the patient id when an image is stored to this table and you are passing a valid patient id to the this action method via id parameter.

Comments