如何创建MVC 4 @ Html.TextBox type ="file"? [英] How to create MVC 4 @Html.TextBox type="file"?
本文介绍了如何创建MVC 4 @ Html.TextBox type ="file"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在表单上添加以下字段
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; }
}
}
并创建
@Html.TextBox(null, null, new { type="file", @class="input-file" })
但是它不起作用,我得到了一些例外.怎么了?
but it doesnt work, I get some exception. What's wrong?
推荐答案
模型
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; }
}
查看
@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
控制器
[HttpPost]
public ActionResult FeedbackForm(FeedbackForm model)
{
// this is your uploaded file
var file = model.ProjectInformation;
...
return View();
}
MVC使用名称约定,因此,如果您的文本框和模型名称匹配,则MVC会将您的输入绑定到模型.
MVC is using name convention, so if your textbox and model names match, then MVC will bind your inputs to your model.
这篇关于如何创建MVC 4 @ Html.TextBox type ="file"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文