12

I need to add the following field at my form

<input type="file" class="input-file" />

I create model and describe this field (the last field)

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace CorePartners_Site2.Models
 {
     public class FeedbackForm
     {
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Company { get; set; }
    public string AdditionalInformation { get; set; }
    public HttpPostedFileBase ProjectInformation { get; set; }
     }
 }

and create

@Html.TextBox(null, null, new { type="file", @class="input-file" })

but it doesnt work, I get some exception. What's wrong?

Heidel
  • 3,174
  • 16
  • 52
  • 84
  • 1
    What error? And why you define null value to name attribute? I mean use like this: `@Html.TextBox("ProjectInformation", null, new { type="file", @class="input-file" })` – AliRıza Adıyahşi May 20 '13 at 07:36
  • yes, I understood now, I create `@Html.TextBox("file", null, new { type="file", @class="input-file" })` and I get `` but I dont need id here. How to create the field without id? – Heidel May 20 '13 at 07:41
  • But if you dont use id you cant bind file to model. And one more suggestion, if you use `ProjectInformation` for id, then mvc will bind file to your model automaticly. – AliRıza Adıyahşi May 20 '13 at 07:44
  • So, I should do that? `@Html.TextBox("ProjectInformation", null, new { type="file", @class="input-file", id="ProjectInformation" })` – Heidel May 20 '13 at 07:47
  • 1
    You should define a name property for htmlHelpers. But you can define what you want for id. I say if you define same like your model, it will bind automaticly. If dont, you should specify it in your controller side – AliRıza Adıyahşi May 20 '13 at 07:50
  • 1
    If you want , I can an write example for you with your model? – AliRıza Adıyahşi May 20 '13 at 07:51
  • yes, please, I'll be cool) – Heidel May 20 '13 at 08:01

7 Answers7

17

Model

public class FeedbackForm
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Company { get; set; }
    public string AdditionalInformation { get; set; }
    public HttpPostedFileBase ProjectInformation { get; set; }
}

View

@model FeedbackForm

@Html.TextBox("Name")
@Html.TextBox("Email")
...
@Html.TextBox("ProjectInformation", null, new { type="file", @class="input-file" })

// submit button

My recommended view (strongly - typed)

@model FeedbackForm

@Html.TextBoxFor(model=>model.Name)
@Html.TextBoxFor(model=>model.Email)
...
@Html.TextBoxFor(model=>model.ProjectInformation, null, new { type="file", @class="input-file" })

// submit button

Controller

[HttpPost]
public ActionResult FeedbackForm(FeedbackForm model)
{
    // this is your uploaded file
    var file = model.ProjectInformation;
    ...

    return View();
}

MVC is using name convention, so if your textbox and model names match, then MVC will bind your inputs to your model.

Cosmin
  • 2,365
  • 2
  • 23
  • 29
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
5

I think you are getting a null because you have not specified the enctype in your form tag.

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

A working example always help.

Visit http://www.mindstick.com/Articles/cf1e1dd9-fdba-4617-94f0-407223574447/?Upload%20File%20in%20Asp.Net%20Mvc%204

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
3

You can use the below syntax

@Html.TextBoxFor(model=>model.Email, new { @type="file", @class="input-file" })
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Tanmay
  • 31
  • 1
2

I solved this problem using enctype="multipart/form-data"

@using (Html.BeginForm("SalvarEvidencia", "Evidencia", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    ...
}
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
1

There's nothing wrong with just using the input tag directly in your view. You aren't required to use a helper.

<input type="file" class="input-file" />

Just make sure it's within your BeginForm declaration block.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

You need to specify the name of the field. If you don't want a name, nor a value, it's better to just include the field as is in your form.

It doesn't make sense to use a helper, if there's nothing dynamic about it.

Kenneth
  • 28,294
  • 6
  • 61
  • 84
-1
  @using (Html.BeginForm("Action_Name", "Controller_Name",FormMethod.Post))
   {
        @Html.TextBoxFor(m => m.Email, new {@class = "text_field"})
        @Html.ValidationMessageFor(m => m.Email)
   }