[英]upload with mvc form
我有一個簡單的問題。 為什么在下面的代碼中,該部分HttpPostedFileBase file
-為空? 當然ArticleModel model
不是null,而只是file
。
我的控制器動作開始:
public ActionResult Add(ArticleModel model, HttpPostedFileBase file)
...
我的表格:
<section id="">
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@ViewBag.Status
<fieldset>
<legend>Add</legend>
<ol>
<li>
@Html.LabelFor(m => m.title)
@Html.TextBoxFor(m => m.title)
</li>
<li>
@Html.LabelFor(m => m.Categories)
@Html.DropDownListFor(m=> m.categoryID, Model.Categories, "- lorem -", new { @class="dropdownlist" })
</li>
<li>
@Html.LabelFor(m => m.connectedArticlesID)
@Html.TextBoxFor(m => m.connectedArticlesID)
</li>
<li>
<input type="file" name="file" id="file" />
</li>
<li>
@Html.LabelFor(m => m.introduction)
@Html.EditorFor(m => m.introduction)
</li>
<li>
@Html.LabelFor(m => m.content)
@Html.EditorFor(m => m.content)
</li>
</ol>
<input type="submit" value="Add" />
</fieldset>
}
</section>
編輯:
我的模特
public class ArticleModel
{
[Display(Name = "Number")]
public int articleID { get; set; }
[Required]
[Display(Name = "Tilte")]
[StringLength(250, ErrorMessage = "Tytuł musi mieć długość od {0} do {2} znaków.", MinimumLength = 6)]
public string title { get; set; }
[Required]
[Display(Name = "Similar articles")]
public string connectedArticlesID { get; set; }
[Display(Name = "CategoryName")]
public string category { get; set; }
[Required]
[Display(Name = "Category")]
public int categoryID { get; set; }
[Required]
[Display(Name = "Category")]
public IEnumerable<SelectListItem> Categories
{
get
{
return new[]
{
new SelectListItem { Value = "1", Text = "Kategoria pierwsza" },
new SelectListItem { Value = "2", Text = "Kategoria druga" },
new SelectListItem { Value = "3", Text = "Kategoria trzecia" },
};
}
}
[Display(Name = "Content")]
[Required]
[StringLength(6000, ErrorMessage = "Treść musi mieć długość od {1} do {2} znaków.", MinimumLength = 30)]
[UIHint("tinymce_jquery_full"), AllowHtml]
public string content { get; set; }
[Display(Name = "Introduction")]
[Required]
[StringLength(6000, ErrorMessage = "Wstęp musi mieć długość od {1} do {2} znaków.", MinimumLength = 30)]
[DataType(DataType.MultilineText)]
public string introduction { get; set; }
}
我有一個簡單的問題。 為什么在下面的代碼中,該部分-HttpPostedFileBase文件-為空?
因為您沒有在multipart/form-data
上將enctype
設置為multipart/form-data
,所以:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}
參考: http : //haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
在將Html.BeginForm
切換到Ajax.BeginForm
並詢問為什么HttpPostedFileBase file - is null
,答案是您不能使用AJAX上傳文件。 您將必須使用文件上傳插件,例如Uploadify或Blueimp File upload。 我只是在記錄中提及此內容,以避免進一步的重復問題。
更新:
根據注釋部分的要求,您可以將文件字段添加到視圖模型中:
public class ArticleModel
{
[Required]
public HttpPostedFileBase File { get; set; }
...
}
然后讓您的控制器操作直接將此視圖模型用作參數並擺脫HttpPostedFileBase參數:
public ActionResult Add(ArticleModel model)
{
if (ModelState.IsValid)
{
... work with model.File directly here
}
}
另外,為了避免您的域模型和視圖模型之間有任何歧義,我將在它們后面加上ViewModel:
public class ArticleViewModel
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.