【问题标题】:File input with HttpPostedFileBase returns always null使用 HttpPostedFileBase 的文件输入始终返回 null
【发布时间】:2013-05-07 12:26:47
【问题描述】:

我们有非常简单的文件输入表单。当我们发布表单时,我们可以处理模型中的其他元素。但是文件输入总是返回null。 顺便说一句,我们在 Telerik Sitefinity 中使用我们的视图和控制器作为自定义控件。这可能与此有关,因为我们找不到任何解决方案。

这里是查看代码

@using (Html.BeginForm("Index", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form">
    <dl>
        <dd>
            @Html.LabelFor(model => model.ShareRecipe.Name)
            @Html.EditorFor(model => model.ShareRecipe.Name)
        </dd>
        <dd>
            @Html.LabelFor(model => model.ShareRecipe.EmailAddress)
            @Html.EditorFor(model => model.ShareRecipe.EmailAddress)
        </dd>
        <dd>
            @Html.LabelFor(model => model.ShareRecipe.RecipeArea)
            @Html.EditorFor(model => model.ShareRecipe.RecipeArea)
        </dd>
        <dd>
            <input type="file" name="fileUpload" id="fileUpload" />
        </dd>
        <dd class="last">
            <input type="submit" class="btn btn-danger" onclick="return ValidateForm();" value="Send" /></dd>
    </dl>
</div>
}  

这是我们的控制器端。

        [HttpPost]
        public ActionResult Index(ShareRecipeModel model, HttpPostedFileBase fileUpload, FormCollection values)
        {
                if (fileUpload != null && fileUpload.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(fileUpload.FileName);
                    var path = Path.Combine(Server.MapPath("~/img/"), fileName);
                    fileUpload.SaveAs(path);
                    model.ImageUrl = fileName;
                }
}

我们也尝试使用基本的表单标签发布。喜欢:

<form method="post" enctype="multipart/form-data"></form>

它不工作。 同样在 Html.BeginForm 我们尝试了所有变体索引和控制器名称,例如:

null,null
"Index","ShareRecipeController"

等等

最后,我想提供有关在 Telerik Sitefinity 中使用自定义控件的信息 我们的控制器上有这一行:

[ControllerToolboxItem(Name = "ShareRecipe", Title = "Share Your Recipe", SectionName = "MvcWidgets")]
public class ShareRecipeController : BaseController

如果有人有任何想法,这对我们来说会很棒, 提前致谢, 塞尔哈德。

【问题讨论】:

    标签: c# asp.net-mvc-3 file-upload telerik sitefinity-5


    【解决方案1】:

    在我们的表单标签中,我们有必要的元素“FormMethod.Post, new { enctype = "multipart/form-data" }" 但由于站点有限结构,当我们将自定义控件用作页面中的自定义用户控件时。它转到该页面的相关母版页。母版页中的表单标签会影响我们在自定义控件中的表单标签。

    为避免此问题,请在母版页标记中添加“method="post" enctype="multipart/form-data" 属性,而不是添加自定义控件视图。

    参考:http://www.sitefinity.com/developer-network/forums/bugs-issues-/upload-file-with-mvc

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 2016-05-31
      • 2015-08-15
      • 2012-03-18
      • 2016-11-04
      相关资源
      最近更新 更多