2013-03-10 72 views
0

這用於正常工作,但現在代碼未將路徑名保存到SitePlanSiteImage字段。如果我調試一切看起來不錯,並且SitePlanSiteImage包含上傳文件的路徑名稱(即〜/ UploadedFiles/20110210104108SiteImage77.jpg)。但是一旦保存在SitePlanSiteImage字段中就是字符串值「System.Web.HttpPostedFileWrapper」。正在保存System.Web.HttpPostedFileWrapper字符串,而不是文件路徑名

因爲它保存得很好,並且正在查看和調試正在保存的值,所以沒有錯誤,並且所有工作似乎都正常,只是數據庫沒有路徑,只是這個字符串「System.Web.HttpPostedFileWrapper」而掙扎着。任何意見不勝感激

這是我的控制器代碼:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult SiteLocationEdit(int id, FormCollection collection) 
{ 
    SiteLocation siteLocation = this._siteRepository.GetSite(Convert.ToInt16(collection["SiteId"])).SiteLocation;   

    if (Request.Files.Count > 0 && Request.Files["SitePlan"].ContentLength > 0) 
    { 
     DeleteFile(siteLocation.SitePlan); 
     siteLocation.SitePlan = SaveFile(Request.Files["SitePlan"], @"~/UploadedFiles", "SitePlan" + siteLocation.SiteId.ToString()); 
    } 

    if (Request.Files.Count > 0 && Request.Files["SiteImage"].ContentLength > 0) 
    { 
     DeleteFile(siteLocation.SiteImage); 
     siteLocation.SiteImage = SaveFile(Request.Files["SiteImage"], @"~/UploadedFiles", "SiteImage" + siteLocation.SiteId.ToString()); 
    } 

    TryUpdateModel(siteLocation); 

    if (!ModelState.IsValid) 
     return View(siteLocation); 


    this._siteRepository.Save(User.Identity.Name); 
    return RedirectToAction("SiteLocationDetails", new { id = siteLocation.SiteId });         
} 

這是我的含有局部視圖(稍後在這篇文章中示出)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Lms.Model.SiteLocation>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    <%= Html.Encode(Model.Site.SiteDescription) %> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<%=Html.Script("~/Scripts/jquery.textarea-expander.js")%> 

    <%= Html.ValidationSummary("Location Create was unsuccessful. Please correct the errors and try again.\n If you uploaded images during this update please upload again.")%> 
    <% using (

     Html.BeginForm 
     (
      "SiteLocationEdit", 
      "Site", 
      FormMethod.Post, 
      // add an encoding type attribute 
      // that is required for file upload 
      new { enctype = "multipart/form-data" } 
     ) 

      ) 
     {%> 
     <% Html.RenderPartial("SiteTabs", Model.Site); %> 
     <div class="clear"> 
     </div> 
     <% Html.RenderPartial("SiteLocationForm", Model); %> 


    <% } %> 

     <script type="text/javascript"> 
      /* jQuery textarea resizer plugin usage */ 
      $(document).ready(function() { 
       jQuery("textarea[class*=expand]").TextAreaExpander(); // initialize all expanding textareas, new code, john s 10/08/2010 
      }); 
     </script> 

</asp:Content> 

這裏View是局部視圖:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Lms.Model.SiteLocation>" %> 
<fieldset> 
    <div> 
     <h3> 
      <label id="Label2" style="text-align: left"> 
       <%= "Site Location: " + Html.Encode(Model.Site.SiteDescription) %></label> 
     </h3> 
    </div> 
    <div class="formFields"> 
     <ul> 
      <%--This is used to identify the site object to update when the model is returned to the controller--%> 
      <%= Html.Hidden("SiteId", Model.SiteId)%> 
      <li> 
       <label for="Latitude"> 
        <strong>Latitude:</strong></label> 
       <%= Html.TextBox("Latitude")%> 
       <%= Html.ValidationMessage("Latitude", "*")%> 
      </li> 
      <li> 
       <label for="Longitude"> 
        <strong>Longitude:</strong></label> 
       <%= Html.TextBox("Longitude") %> 
       <%= Html.ValidationMessage("Longitude", "*") %> 
      </li> 
      <li> 
       <label for="Location"> 
        <strong>Location Address:</strong></label> 
       <label><%= Html.TextArea("Location", Model.Location, new { @class = "expand50-200"}) %></label> 
       <%= Html.ValidationMessage("Location", "*") %> 
      </li> 
      <li> 
       <label for="NearestPostcode"> 
        <strong>Nearest Postcode:</strong></label> 
       <%= Html.TextBox("NearestPostcode") %> 
       <%= Html.ValidationMessage("NearestPostcode", "*") %> 
      </li> 
      <li> 
       <label for="TimeFromOfficeToTurbine"> 
        <strong>Office to Windfarm (Time):</strong></label> 
       <%= Html.TextBox("TimeFromOfficeToTurbine") %> 
       <%= Html.ValidationMessage("TimeFromOfficeToTurbine", "*") %> 
      </li> 
      <li> 
       <label for="Directions"> 
        <strong>Comments:</strong></label> 
       <label><%= Html.TextArea("Directions", Model.Directions, new { @class = "expand50-200" })%> </label> 
       <%= Html.ValidationMessage("Directions", "*") %> 
      </li> 

      <li> 
      <h5><strong>For Image Uploads:</strong> Please use only JPG,JPEG or GIF formats. 
      Image size should be appropriate for the webpage to display, approximately 500x375 (WidthxHeight) 
      </h5> 
      </li> 

      <li> 
       <label for="Site Plan"> 
        <strong>Site Plan:</strong></label> 
       <input id="SitePlan" name="SitePlan" type="file" /> 
      </li> 

      <li> 
       <label for="Site Image"> 
        <strong>Site Image:</strong></label> 
       <%--   <%//= Html.TextBox("SiteImage", Model.SiteImage)%> 
      <%//= Html.ValidationMessage("SiteImage", "*") %>--%> 
       <input id="SiteImage" name="SiteImage" type="file" /> 
      </li> 


     </ul> 
    </div> 
</fieldset> 
<div class='demo'> 
    <%=Html.ActionLink("< Back to List", "Index") %><input type="submit" value="Save" /> 
</div> 

這裏是保存文件過程中的控制器代碼:

protected String SaveFile(HttpPostedFileBase file, String path, string name) 
{ 
    if (file != null && file.ContentLength > 0) 
    { 
     if (path == null) 
     { 
      throw new ArgumentNullException("path cannot be null"); 
     } 

     string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."), file.FileName.Length - file.FileName.LastIndexOf(".")); 

     String relpath = String.Format("{0}/{1}", path, PrefixFName(name + fileType)); 
     try 
     { 
      file.SaveAs(Server.MapPath(relpath)); 
      return relpath; 
     } 
     catch (HttpException e) 
     { 
      throw new ApplicationException("Cannot save uploaded file", e); 
     } 
    } 
    return null; 
} 

這裏是SiteLocationCreate()這工作,只是沒有了編輯:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult SiteLocationCreate(SiteLocation siteLocation, FormCollection collection) 
{ 
    // TODO CF: Look into this line. Is there a better way to do it? I would think so. 
    // It uses a hidden field in the object form 
    Site site = this._siteRepository.GetSite(Convert.ToInt16(collection["SiteId"]));       
    site.SiteLocation = siteLocation; 

    if (Request.Files.Count > 0 && Request.Files["SitePlan"].ContentLength > 0) 
    { 
     DeleteFile(siteLocation.SitePlan); 
     siteLocation.SitePlan = SaveFile(Request.Files["SitePlan"], @"~/UploadedFiles", "SitePlan" + siteLocation.SiteId.ToString()); 
    } 

    if (Request.Files.Count > 0 && Request.Files["SiteImage"].ContentLength > 0) 
    { 
     DeleteFile(siteLocation.SiteImage); 
     siteLocation.SiteImage = SaveFile(Request.Files["SiteImage"], @"~/UploadedFiles", "SiteImage" + siteLocation.SiteId.ToString()); 
    } 

    if (!ModelState.IsValid)    
     return View(siteLocation);        

    this._siteRepository.Save(User.Identity.Name); 
    return RedirectToAction("SiteLocationDetails", new { id = site.SiteId });         
} 
+0

您能否顯示'SaveFile()'的代碼?我認爲這是你自己的代碼。 – 2013-03-10 02:49:47

+0

更新完成,SaveFile()添加,歡呼 – John 2013-03-10 03:34:40

+0

現貨,就是這樣,在這裏去香蕉:-) Bizzarly SiteLocationCreate()工作,張貼它現在發佈 – John 2013-03-10 04:24:50

回答

0

刪除TryUpdateModel()線和它的工作。但必須爲每個字段更新逐行指定,即siteLocation.Location = collection [「Location」];由於TryUpdateModel已被取出

+0

很高興你明白了! – 2013-03-10 04:43:02

相關問題