Home > DeveloperSection > Forums > How to Attachments in mvc?
lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    October-09-2014 12:51 AM

 ASP.NET MVC ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 830  View(s)
Rate this:

I have to update my contact form, to allow attachments.

My reading indicates I need to add a new property to my model, of type HttpPostedFileBase

So, I did the following

@model Ui.Models.Email

 

    @using (Html.BeginForm())

    {

        @Html.ValidationSummary(true)

 

        @Html.TextBoxFor(a => a.Attachment, new { @type = "file" })

    }

And my model

public class Email

        {

            [Display(Name = "Attach away my assuming chum")]

            public HttpPostedFileBase Attachment { get; set; }

        }

The problem I'm getting is the property is always null!

Usually, when it's a string, int or List, the binding works, but I have no idea how to get it to bind to my model.



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-09-2014 12:52 AM

To allow your form to upload files, it should have the enctype="multipart/form-data" attribute.

You can do it like so.

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new{ enctype="multipart/form-data" }))

{

    @Html.ValidationSummary(true)

 

    @Html.TextBoxFor(a => a.Attachment, new { @type = "file" })

}


Don't want to miss updates? Please click the below button!

Follow MindStick