gpt4 book ai didi

c# - 以mvc形式上传

转载 作者:太空宇宙 更新时间:2023-11-03 19:15:32 25 4
gpt4 key购买 nike

我有一个简单的问题。为什么在下面的代码中,这部分 - HttpPostedFileBase file - 为空?当然ArticleModel模型不是null,只是file

我的 Controller Action 开始:

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; }
}

最佳答案

I have a simple question. Why in code below, this part - HttpPostedFileBase file - is null?

因为您没有在表单上将 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 文件 - 为空之前,答案是您不能使用 AJAX 上传文件。您将不得不使用文件上传插件,例如 Uploadify 或 Blueimp 文件上传。我只是为了记录而提及这一点,以避免进一步可能出现的重复问题。


更新:

根据评论部分的要求,您可以将文件字段添加到您的 View 模型中:

public class ArticleModel
{
[Required]
public HttpPostedFileBase File { get; set; }

...
}

然后让您的 Controller 操作直接将此 View 模型作为参数并摆脱 HttpPostedFileBase 参数:

public ActionResult Add(ArticleModel model)
{
if (ModelState.IsValid)
{
... work with model.File directly here
}
}

同时为了避免域模型和 View 模型之间的任何歧义,我会在它们后面加上 ViewModel:

public class ArticleViewModel
{
...
}

关于c# - 以mvc形式上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17148942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com