asp.net-mvc 如何创建 MVC 4 @Html.TextBox type="file"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16644639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:51:34  来源:igfitidea点击:

How to create MVC 4 @Html.TextBox type="file"?

asp.net-mvcasp.net-mvc-4

提问by Heidel

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?

但它不起作用,我有一些例外。怎么了?

回答by AliR?za Ad?yah?i

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.

MVC 使用命名约定,因此如果您的文本框和模型名称匹配,那么 MVC 会将您的输入绑定到您的模型。

回答by Julius Depulla

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

我认为你得到了一个空值,因为你没有在你的表单标签中指定 enctype。

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

@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

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

回答by Tanmay

You can use the below syntax

您可以使用以下语法

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

回答by Henrique Brand?o

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

我解决了这个问题 enctype="multipart/form-data"

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

回答by Erik Funkenbusch

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

直接在视图中使用 input 标签并没有错。您不需要使用助手。

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

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

只需确保它在您的 BeginForm 声明块内。

回答by Kenneth

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.

如果没有任何动态,使用助手是没有意义的。

回答by user3452580

  @using (Html.BeginForm("Action_Name", "Controller_Name",FormMethod.Post))
   {
        @Html.TextBoxFor(m => m.Email, new {@class = "text_field"})
        @Html.ValidationMessageFor(m => m.Email)
   }