Gudbjorn Einarsson
Gudbjorn Einarsson

Reputation:

Uploading an image in ASP.NET MVC using EDM

I am new to using ASP.NET MVC and I've been having some difficulties with it. I created my model using the Entity Data Model just for the record.

My problem is that I'm trying to insert an image into my MS SQL 2008 server using my new ASP.NET page. The image is a part of the information that I need to store about Employees in my database. From my home HomeController.cs file I right-clicked the Create method and chose to create a view, which it did. However, uploading an image was not part of the auto-generated code.

In the Create View there are a few text-boxes that correspond to my table columns and a single submit button that creates my new database row somehow. I thought that the button would run the code for the other Create method in the HomeController.cs file that takes an Employee as a parameter but I guess my problem lies in the fact that I don't know what happens between me pressing the Submit button and the creation of an Employee object for the Create method.

For reference, here is the code I'm working with .First code for Homecontroller.cs and then code for Create.aspx (note that the variable _db is a copy of my Entity Data Model):

public ActionResult Create()
{
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]  
public ActionResult Create([Bind(Exclude = "employeeNumber")] Employee employeeToCreate)  
{  
    try  
    {  
        _db.AddToEmployee(employeeToCreate);  
        _db.SaveChanges();  

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

I don't know why the code above indent's the way it does. I also don't know why, but the code below doesn't show the first few lines properly so I deleted a few < > around C# code and asp code in order for the code to become visible.(FIXED)

<Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<LaunakerfiASP_MVC.Models.Employee>"
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Create</h2>
    <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
    <% using (Html.BeginForm()) { %>
    <fieldset>
        <legend>Fields</legend>          
        <p>
            <label for="name">name:</label>
            <%= Html.TextBox("name")%>
            <%= Html.ValidationMessage("name", "*")%>
        </p>
        <p>
            <label for="email">email:</label>
            <%= Html.TextBox("email")%>
            <%= Html.ValidationMessage("email", "*")%>
        </p>
        <p>
            <label for="department">department:</label>
            <%= Html.TextBox("department")%>
            <%= Html.ValidationMessage("department", "*")%>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>    
    <% } %>
    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>
</asp:Content>

So what I would like to do is to add an element to the Create.aspx view that let's me upload an image to the column 'picture' in the table 'Employee'.

Upvotes: 0

Views: 755

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292465

In your controller action, you can access the uploaded files through Request.Files

Here are some links about file uploads in ASP.NET MVC :

http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

http://aspzone.com/tech/asp-net-mvc-file-upload/

Upvotes: 1

Related Questions