Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I was wondering if anybody knows how to Display an image from a database in a MVC view? At the moment I have an upload view which allows a user to upload an image and all these images are displayed in a webgrid in a Edit view with a "Delete" button next to image record. I want the Delete button to take the user to a new view where the image they have clicked on is displayed so they know they are deleting the correct one,

I have set up separate views and controllers for the new "DeleteItemImage" function which will display the image, but all are empty at the moment,

Any help would be appreciated,

Nick

Here is my code for the Images webgrid in the Edit view:

Razor
        <table style="width:100%">
              @{
var imagesGrid = new WebGrid(Model.ItemImages);
               }
                @imagesGrid.GetHtml(tableStyle: "webgrid",
        headerStyle: "webgrid-header",
        footerStyle: "webgrid-footer",
        alternatingRowStyle: "webgrid-alternating-row",
        selectedRowStyle: "webgrid-selected-row",
        rowStyle: "webgrid-row-style",
        (columns: imagesGrid.Columns(
                imagesGrid.Column("Sequence_No_", header: "Sequence No"),
                imagesGrid.Column("Image_Title", header: "Image Title"),
                imagesGrid.Column("Main_Image", header: "Primary Image"),
                imagesGrid.Column("Uploaded", header: "Uploaded to LinnWorks"),
                imagesGrid.Column("",
            format: @<text>
                     @Html.ActionLink("Delete", "DeleteItemImage");
                    </text>),
                imagesGrid.Column("",
            format:@<text>
                    @Html.ActionLink("Select", null, new { selectImage = Model.SelectImage })
                </text>),
            imagesGrid.Column("",
            format:@<text>
                    @Html.ActionLink("Make Primary", null, new { makePrimary = Model.MakeMainImage })
                    </text>
            ))))

        </table>


Here is my code for the Edit action result:
C#
public ActionResult Edit(string itemNo)
{
        LinnWorksItemViewModel model = new LinnWorksItemViewModel(_data.Connection.ConnectionString, itemNo);
        return View(model);

        foreach (string upload in Request.Files)
        {
            if (!Request.Files[upload].HasFile()) continue;
            string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
            string fileName = Path.GetFileName(Request.Files[upload].FileName);
            Request.Files[upload].SaveAs(Path.Combine(path, fileName));
        }
        return View(model);
}
Posted

1 solution

One bug in your action result is that your second
return view(model); 

will not work, because when you return a model , you can't return another model!
 
Share this answer
 
v2
Comments
Makin6425 17-Sep-13 11:12am    
Yes, sorry I see now, I will remove for first "return view(model)" as it is not needed, thank you for pointing that out!
Mehdy Moini 17-Sep-13 12:21pm    
please accept solution if solve your problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900