2016-05-31 41 views
1

I'm做我想要的擴展方法我的變量image等於我的照片無法訪問的變量在數據庫中保存

擴展方法的名稱:

public async Task<string> CreateNewSlider(Slider slider, HttpPostedFileBase file) // file always return null 
    { 
     string pic = null; 
     if (file != null) 
     { 
      pic = System.IO.Path.GetFileName(file.FileName); 
      string path = System.IO.Path.Combine(
            System.Web.HttpContext.Current.Server.MapPath("~/Content/images/slider"), pic); 
      file.SaveAs(path); 

      using (var ms = new MemoryStream()) 
      { 
       file.InputStream.CopyTo(ms); 
       byte[] array = ms.GetBuffer(); 
      } 
     } 
     try 
     { 
      var createnewslider = new Slider 
      { 
       Alt = slider.Alt, 
       CreationDate = slider.CreationDate, 
       Description = slider.Description, 
       IsVisible = slider.IsVisible, 
       Order = slider.Order, 
       Subtitle = slider.Subtitle, 
       Title = slider.Title, 
       VideoLink = slider.VideoLink, 
       Image = pic 
      }; 
      db.SlidersList.Add(createnewslider); 
      await db.SaveChangesAsync(); 
      return "Slider Photo " + file + "has been created successfull"; 

     } 
     catch (Exception ex) 
     { 
      return ex.InnerException.Message + "Contact to administrator"; 
     } 

    } 

我該怎麼做才能訪問我的變量pic終於發佈到我的模型

注:「圖片變量的類型爲字符串」

----------------------編輯------ ----------------

我試圖創造新的滑塊上的問題,我想創建並存儲圖像到數據庫

模型圖像變量:

public class Slider 
{ 
    public int SliderId { get; set; } 
    public int MainPageId { get; set; } 
    public MainPage MainPage { get; set; } 
    public string Title { get; set; } 
    public string Order { get; set; } 
    public string Subtitle { get; set; } 
    public string Description { get; set; } 
    public string Image { get; set; } 
    public string Alt { get; set; } 
    public string VideoLink { get; set; } 
    public DateTime CreationDate { get; set; } 
    public bool IsVisible { get; set; } 
} 

創建視圖:

@model xxx.Models.Slider 

@{ 
ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Slider</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 


    <div class="form-group"> 
     @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Order, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Order, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Order, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Subtitle, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Subtitle, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Subtitle, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @using (Html.BeginForm("Create", "Slider", FormMethod.Post, 
         new { enctype = "multipart/form-data" })) 
      { 
      <label for="file">Subir imágen:</label> 
      <input type="file" name="file" id="file" style="width: 100%;" /> 
      } 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Alt, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Alt, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Alt, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.VideoLink, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.VideoLink, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.VideoLink, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.CreationDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.CreationDate, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.CreationDate, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.IsVisible, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <div class="checkbox"> 
       @Html.EditorFor(model => model.IsVisible) 
       @Html.ValidationMessageFor(model => model.IsVisible, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

<div> 
@Html.ActionLink("Back to List", "Index") 
</div> 

在擴展方法將它傳遞給趕上異常並返回它

我使用調試模式,並且文件變量始終爲空,但我不知道爲什麼

+0

你在做'Extension Method' ...?我沒有看到你的方法中的第一個參數用'this'限定。 – MethodMan

+0

在你檢查的文件條件內寫模型代碼,否則在文件條件之上聲明圖片變量。 –

+0

我這樣做,有我的模型和視圖@AkramKhan,現在它傳遞給異常參考,我不知道爲什麼 –

回答

0

這是因爲pic在您的createnewslider對象的範圍中不可用。簡單地聲明pic變量if語句外:

string pic = string.Empty; 

if (file != null) { 
    pic = System.IO.Path.GetFileName(file.FileName); 
    string path = System.IO.Path.Combine(
    HostingEnvironment.MapPath("~/Content/images/slider"), pic); 
    file.SaveAs(path); 

    using (MemoryStream ms = new MemoryStream()) { 
     file.InputStream.CopyTo(ms); 
     byte[] array = ms.GetBuffer(); 
    } 
} 
var createnewslider = new Slider 
{ 
    Alt = slider.Alt, 
    CreationDate = slider.CreationDate, 
    Description = slider.Description, 
    IsVisible = slider.IsVisible, 
    Order = slider.Order, 
    Subtitle = slider.Subtitle, 
    Title = slider.Title, 
    VideoLink = slider.VideoLink, 
    Image = pic 
}; 

並檢查映射之前的內容Image

+0

我這樣做,但我提交不保存在數據庫中 –

+0

我上傳模型,擴展方法和查看 –

0

在與構造函數相同的範圍內定義它,以便構造函數可以訪問它。

public async Task<string> CreateNewSlider(Slider slider, HttpPostedFileBase file) 
{ 
    string pic = null; 
    if (file != null) 
    { 
     pic = System.IO.Path.GetFileName(file.FileName); 
     string path = System.IO.Path.Combine(
           HostingEnvironment.MapPath("~/Content/images/slider"), pic); 
     file.SaveAs(path); 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      file.InputStream.CopyTo(ms); 
      byte[] array = ms.GetBuffer(); 
     } 
    } 
    var createnewslider = new Slider 
    { 
     Alt = slider.Alt, 
     CreationDate = slider.CreationDate, 
     Description = slider.Description, 
     IsVisible = slider.IsVisible, 
     Order = slider.Order, 
     Subtitle = slider.Subtitle, 
     Title = slider.Title, 
     VideoLink = slider.VideoLink, 
     Image = pic // I can´t access to pic variable 

    }; 
    db.SlidersList.Add(createnewslider); 
    await db.SaveChangesAsync(); 
    return "Slider Photo " + file + "has been created"; 

} 
+0

你能檢查我的編輯嗎? –