出处:http://www.cnblogs.com/zhouhb/p/3906714.html
最近参考网络资料,学习了ASP.NET MVC如何上传文件。最基本的,没有用jQuery等技术。
1、定义Model
public class TestModel
{
[Display(Name = "标题")]
[Required]
public string Title
{
get;
set;
}
[Display(Name = "内容")]
[Required]
[DataType(DataType.MultilineText)]
public string Content
{
get;
set;
}
public string AttachmentPath
{
get;
set;
}
}
2、Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
3、View
3.1 Upload.cshtml
|
3.2 Show.cshtml
@model MvcApplication1.Models.TestModel
@{
ViewBag.Title =
"Show"
;
}
<h2>Show</h2>
@Html.LabelFor(mod => mod.Title)
<br />
@Html.EditorFor(mod => mod.Title)
<br /> <br />
@Html.LabelFor(mod => mod.Content)
<br />
@Html.EditorFor(mod => Model.Content)
<br />
图片:
<img src=
"@Model.AttachmentPath"
alt=
"img"
/>