I have a simple question. Why in code below, this part - HttpPostedFileBase file
- is null? Of course ArticleModel model
is not null, just file
.
Begin of my controler action:
public ActionResult Add(ArticleModel model, HttpPostedFileBase file)
...
My form:
<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>
EDIT:
my model
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; }
}
I have a simple question. Why in code below, this part - HttpPostedFileBase file - is null?
Because you didn't set the enctype
to multipart/form-data
on your form:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}
Reference: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
And before you switch your Html.BeginForm
to an Ajax.BeginForm
and then ask a question why HttpPostedFileBase file - is null
, the answer is you cannot upload files using AJAX. You will have to use a file upload plugin such as Uploadify or Blueimp File upload. I am just mentioning this for the record in order to avoid further possible duplicate questions.
UPDATE:
As requested in the comments section you could add the file field to your view model:
public class ArticleModel
{
[Required]
public HttpPostedFileBase File { get; set; }
...
}
and then have your controller action directly take this view model as argument and get rid of the HttpPostedFileBase argument:
public ActionResult Add(ArticleModel model)
{
if (ModelState.IsValid)
{
... work with model.File directly here
}
}
Also to avoid any ambiguity between your domain models and view models, I would suffix them with ViewModel:
public class ArticleViewModel
{
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.