HttpPostedFileBase将为null

时间:2016-04-05 13:02:38

标签: asp.net-mvc controller http-post

下面的

是我的视图中的代码

@using ClaimBuildMVC.Models
@model IEnumerable<Asset>
@{
    ViewBag.Title = "AssetsPage";
    Layout = "~/Views/Shared/_SuperAdminLayout.cshtml";
}
<script type="text/javascript">      
</script>
@using (Html.BeginForm("AssetsPage", "SuperAdmin", FormMethod.Post,new{enctype = "multipart/form-data"}))
{     
<div class="Content-inner-pages">
<div class="TopHeading TopHeading2">
    <h2>Assets</h2>        
    <a class="CreateBtn AssetsBtn" href="Javascript:void(0);" onclick="javascript:$('#hdnIsNew').val('1')">Add Asset</a>
    <div class="clearfix"></div>
</div>
<input type="hidden" id="hdnIsNew" value="1" />
<input type="hidden" id="hdnRecId" />
<!-- Slide Popup panel -->
<div class="cd-panel from-right AddAssetForm">
    <header class="cd-panel-header">
        <h3>Add Asset</h3>
        <a href="javascript:void(0);" onclick="javascript: DisplayClear();" class="cd-panel-close">Close</a>
    </header>
    <div class="cd-panel-container">
        <div class="cd-panel-content">                
            <div class="form-horizontal form-details popup-box">
                <div class="form-group">
                    <label class="col-md-5 control-label">
                        Asset Title
                    </label>
                    <div class="col-md-7">
                        <input type="text" id="txtTitle" name="txtTitle" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-5 control-label">Description</label>
                    <div class="col-md-7">
                        <textarea id="txtDesc" class="form-control" cols="5" rows="5"></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-5 control-label">Attachment</label>                        
                    <div class="col-md-7">
                        <input type="file" id="file" class="custom-file-input">
                    </div>                        
                </div>
                <div class="form-group">
                    <div class="col-md-7 col-md-offset-5">
                        <input type="submit" value="Save" name="actionType" class="btn-class btn-success">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="box">
    <div class="box-content Custom-DataTable">
        <table id="AdministationAssets" class="table table-hover dt-responsive CustomDatable AdministationAssetsTable" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th style="width:5%;">Assets</th>
                    <th style="width:15%;">
                        @Html.DisplayNameFor(model =>model.Title)
                    </th>
                    <th style="width:50%;">
                        @Html.DisplayNameFor(model =>model.Description)
                    </th>
                    <th style="width:8%;">Options</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                    {
                    <tr>
                        <td></td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Title)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem =>item.Description)
                        </td>
                        <td>
                            @Html.ActionLink("Edit", "AddEditRecord", new{ id = item.ID }, new { @class = "ActionEdit AssetEdit", onclick ="javascript:GetEditDetails(" + item.ID + ");" })
                            @Html.ActionLink("Delete", "AssetDelete", new{ id = item.ID }, new { @class = "ActionDelete", onclick = "return confirm('Are You Sure delete this record?');", })
                        </td>
                    </tr>
                        }
            </tbody>
        </table>
    </div>
</div>

以下是我的控制器,当我点击保存按钮时我要调用的内容,但'img'将显示为null并且在google中搜索后我发现要添加@using(Html.BeginForm())但仍然会显示为空< / p>

  [HttpPost]
  public ActionResult AssetsPage(Asset ast, HttpPostedFileBase file)
  {
     using (GenericUnitOfWork gc = new GenericUnitOfWork())
        {                
            if (HttpContext.Request.Files.Count > 0)
            {                  
                ast.ContainerName = "reports";
                ast.BlobName = HttpContext.Request.Files[0].FileName;
                ast.BlobUrl = BlobUtilities.CreateBlob(ast.ContainerName, ast.BlobName, null, GetStream(HttpContext.Request.Files[0].FileName));
                ast.FileName = HttpContext.Request.Files[0].FileName;
            }
            gc.GetRepoInstance<Asset>().Add(ast);
            gc.SaveChanges();
        }
        return RedirectToAction("AssetsPage");
    }

无法找到解决方案。如果可能,请帮助或提供一些参考。

1 个答案:

答案 0 :(得分:0)

Asp.Net MVC 默认模型绑定与name属性一起使用,因此添加name="file"属性input type="file"如下所示: -

<input type="file" name="file" id="file" class="custom-file-input">