0

我有以下_CreateOrEditPartial包含文本和文件上传的部分视图:-

@model TMS.Models.DataCenter
@* This partial view defines form fields that will appear when creating and editing entities *@
@Html.AntiForgeryToken()

<div>
    <span class="f">Name </span>    
    @Html.EditorFor(model=>model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>
<div>
    <span class="f">Data Center Picture </span>    
    <input type="file" name="DataCenterfile" />
</div>

以及以下主要观点:-

@using (Html.BeginForm("Create","DataCenter", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    @Html.Partial("_CreateOrEdit", Model)
    <input type="submit" value="Create" class="btn btn-primary"/>
}

这将调用以下 ActionMethod:-

[HttpPost]
[ValidateAntiForgeryToken]
[CheckUserPermissions(Action = "Edit", Model = "DataCenter")]
public ActionResult Create(DataCenter dc, HttpPostedFileBase DataCenterfile)
{
    // Verify that the user selected a file
    if (DataCenterfile != null && DataCenterfile.ContentLength > 0)
    {
        // extract only the fielname
        var fileName = Path.GetFileName(DataCenterfile.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        DataCenterfile.SaveAs(path);
    }

但数据中心文件将始终为空..任何人都可以建议是什么导致了这个问题?谢谢

4

1 回答 1

2

您忘记将enctype属性添加到表单中。

像这样的东西:

@using (Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

}

关于enctype属性:enctype='multipart/form-data' 是什么意思?

于 2013-11-12T10:18:43.740 回答