2016-04-05 3 views
0

ниже код на мой взглядHttpPostedFileBase приходит нуль

@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» приходит утратившим после поиска в Google я нашел, что добавить @using(Html.BeginForm()), но все-таки приходит утратившим

[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

спасибо @ KartikeyaKhosla..и работает..может вы скопируете вставьте это в раздел ответов, чтобы я мог отметить это как ответ – Steve

+0

рад помочь вам ..;). –

ответ

0

Asp.Net MVC модель по умолчанию связывание работает с name атрибутом, поэтому добавьте name="file" атрибут с input type="file", как показано ниже: -

<input type="file" name="file" id="file" class="custom-file-input"> 
Смежные вопросы