2013-05-03 111 views
0

有沒有人知道如何在mvc asp.net中使用CRUD時上傳圖片,這樣我的圖片就可以上傳到我的頭文件中,無論是在自己的div還是在頭部的特定div中,現在我可以上傳內容中的圖片,但我不知道如何將其上傳到我的logoelement中。workin with images and crud in mvc

這裏是代碼,所以你可以看到,我想它顯示 @model IEnumerable的

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>@ViewBag.Title - My ASP.NET MVC Application</title> 
     <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
     <meta name="viewport" content="width=device-width" /> 
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 
     @Styles.Render("~/Content/css") 
     @Scripts.Render("~/bundles/modernizr") 

    </head> 
<body> 
    <header> 
      <div id="float-logo"><img src="@Model.PicUrl" alt="" /></div> 
      <div id="title"><p class="site-title">@Html.Action("SiteInfo", "Home")</p></div> 
      <div class="float-login"> 
       <section id="login"> 
        @Html.Partial("_LoginPartial") 
       </section> 
      </div> 
       <nav id="MainNav"> 
        <ul> 
         <li>@Html.ActionLink("Index", "Index", "Home")</li> 
         <li>@Html.ActionLink("Rapporter", "Index", "Report")</li> 
        </ul> 
       </nav> 
    </header> 
    <div id="Layout"> 
     <nav id="SideNav"></nav> 
     @RenderSection("featured", required: false) 
     <section class="Content"> 
      @RenderBody() 
     </section> 
    </div> 
    <footer></footer> 

    @Scripts.Render("~/bundles/jquery") 
    @RenderSection("scripts", required: false) 
</body> 

這是控制器的一部分,它創建它:

public class PictureController : Controller 
{ 
    private ItbTidCmsEntities db = new ItbTidCmsEntities(); 

    // 
    // GET: /Picture/ 

    public ActionResult Index() 
    { 
     var pictures = db.Pictures.Include("Category"); 
     return View(pictures.ToList()); 
    } 
    public ActionResult Create(int? id) 
    { 

     ViewBag.CatID = id; 
     var category = db.Categories.First(c => c.CatID == id.Value); 
     ViewBag.CatName = category.CatName; 

     return View(); 
    } 

    // 
    // POST: /Picture/Create 

    [HttpPost] 
    public ActionResult Create(Picture picture) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Pictures.AddObject(picture); 
      db.SaveChanges(); 
      return RedirectToAction("Content", "Admin", new { id = picture.CatID }); 
     } 

     ViewBag.CatID = new SelectList(db.Categories, "CatID", "CatName", picture.CatID); 
     return View(picture); 
    } 

管理模式:

namespace ItbTid.Models 
{ 
    public class Admin 
    { 
     public List<Category> category { get; set; } 
     public List<Text> text { get; set; } 
     public List<Picture> picture { get; set; } 
     public List<Video> video { get; set; } 
     public List<Link> link { get; set; } 
    } 
} 

timeentrylistmodel:

namespace ItbTid.Models 
{ 
    public class TimeEntryListModel 
    { 
    public IEnumerable<TimeEntry> TimeEntries { get; set; } 
    public SelectList Customers { get; set; } 
    public long? CustomerId { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
    } 

    //Vi la till denna 
    public class RegTimeModel 
    { 
     public TimeEntryListModel TimeEntryListModel { get; set; } 
     public RegisterModel RegisterModel { get; set; } 
     public IEnumerable<Customer> customerlist { get; set; } 
    } 
} 

這是從數據庫css_db.designer.cs一塊代碼:

public partial class Picture : EntityObject 
{ 
    #region Factory Method 

    /// <summary> 
    /// Create a new Picture object. 
    /// </summary> 
    /// <param name="picID">Initial value of the PicID property.</param> 
    /// <param name="picUrl">Initial value of the PicUrl property.</param> 
    /// <param name="picAltText">Initial value of the PicAltText property.</param> 
    public static Picture CreatePicture(global::System.Int64 picID, global::System.String picUrl, global::System.String picAltText) 
    { 
     Picture picture = new Picture(); 
     picture.PicID = picID; 
     picture.PicUrl = picUrl; 
     picture.PicAltText = picAltText; 
     return picture; 
    } 

    #endregion 
    #region Primitive Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int64 PicID 
    { 
     get 
     { 
      return _PicID; 
     } 
     set 
     { 
      if (_PicID != value) 
      { 
       OnPicIDChanging(value); 
       ReportPropertyChanging("PicID"); 
       _PicID = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("PicID"); 
       OnPicIDChanged(); 
      } 
     } 
    } 
    private global::System.Int64 _PicID; 
    partial void OnPicIDChanging(global::System.Int64 value); 
    partial void OnPicIDChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public global::System.String PicTitle 
    { 
     get 
     { 
      return _PicTitle; 
     } 
     set 
     { 
      OnPicTitleChanging(value); 
      ReportPropertyChanging("PicTitle"); 
      _PicTitle = StructuralObject.SetValidValue(value, true); 
      ReportPropertyChanged("PicTitle"); 
      OnPicTitleChanged(); 
     } 
    } 
    private global::System.String _PicTitle; 
    partial void OnPicTitleChanging(global::System.String value); 
    partial void OnPicTitleChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String PicUrl 
    { 
     get 
     { 
      return _PicUrl; 
     } 
     set 
     { 
      OnPicUrlChanging(value); 
      ReportPropertyChanging("PicUrl"); 
      _PicUrl = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("PicUrl"); 
      OnPicUrlChanged(); 
     } 
    } 
    private global::System.String _PicUrl; 
    partial void OnPicUrlChanging(global::System.String value); 
    partial void OnPicUrlChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String PicAltText 
    { 
     get 
     { 
      return _PicAltText; 
     } 
     set 
     { 
      OnPicAltTextChanging(value); 
      ReportPropertyChanging("PicAltText"); 
      _PicAltText = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("PicAltText"); 
      OnPicAltTextChanged(); 
     } 
    } 
    private global::System.String _PicAltText; 
    partial void OnPicAltTextChanging(global::System.String value); 
    partial void OnPicAltTextChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public global::System.String PicDesc 
    { 
     get 
     { 
      return _PicDesc; 
     } 
     set 
     { 
      OnPicDescChanging(value); 
      ReportPropertyChanging("PicDesc"); 
      _PicDesc = StructuralObject.SetValidValue(value, true); 
      ReportPropertyChanged("PicDesc"); 
      OnPicDescChanged(); 
     } 
    } 
    private global::System.String _PicDesc; 
    partial void OnPicDescChanging(global::System.String value); 
    partial void OnPicDescChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.Int64> PicPrio 
    { 
     get 
     { 
      return _PicPrio; 
     } 
     set 
     { 
      OnPicPrioChanging(value); 
      ReportPropertyChanging("PicPrio"); 
      _PicPrio = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("PicPrio"); 
      OnPicPrioChanged(); 
     } 
    } 
    private Nullable<global::System.Int64> _PicPrio; 
    partial void OnPicPrioChanging(Nullable<global::System.Int64> value); 
    partial void OnPicPrioChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.Int64> CatID 
    { 
     get 
     { 
      return _CatID; 
     } 
     set 
     { 
      OnCatIDChanging(value); 
      ReportPropertyChanging("CatID"); 
      _CatID = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("CatID"); 
      OnCatIDChanged(); 
     } 
    } 
    private Nullable<global::System.Int64> _CatID; 
    partial void OnCatIDChanging(Nullable<global::System.Int64> value); 
    partial void OnCatIDChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public global::System.Byte[] PicDateCreated 
    { 
     get 
     { 
      return StructuralObject.GetValidValue(_PicDateCreated); 
     } 
     set 
     { 
      OnPicDateCreatedChanging(value); 
      ReportPropertyChanging("PicDateCreated"); 
      _PicDateCreated = StructuralObject.SetValidValue(value, true); 
      ReportPropertyChanged("PicDateCreated"); 
      OnPicDateCreatedChanged(); 
     } 
    } 
    private global::System.Byte[] _PicDateCreated; 
    partial void OnPicDateCreatedChanging(global::System.Byte[] value); 
    partial void OnPicDateCreatedChanged(); 

    #endregion 

    #region Navigation Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [XmlIgnoreAttribute()] 
    [SoapIgnoreAttribute()] 
    [DataMemberAttribute()] 
    [EdmRelationshipNavigationPropertyAttribute("ItbTidCmsModel", "FK_Pictures_Categories", "Categories")] 
    public Category Category 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("ItbTidCmsModel.FK_Pictures_Categories", "Categories").Value; 
     } 
     set 
     { 
      ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("ItbTidCmsModel.FK_Pictures_Categories", "Categories").Value = value; 
     } 
    } 
    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [BrowsableAttribute(false)] 
    [DataMemberAttribute()] 
    public EntityReference<Category> CategoryReference 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("ItbTidCmsModel.FK_Pictures_Categories", "Categories"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Category>("ItbTidCmsModel.FK_Pictures_Categories", "Categories", value); 
      } 
     } 
    } 

    #endregion 
} 

回答

0

我想你希望它顯示在頁面的左上方。只需將您的圖片放入<div id="title">即可。

+0

如果我把我的URL圖像在div id浮標,然後即時通訊異常獲取錯誤的異常詳細信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:'ItbTid.Models.TimeEntryListModel'不包含'PicUrl' – user2043267 2013-05-03 13:03:44

+0

定義你粘貼你的問題,你怎麼嘗試過 – 2013-05-03 13:11:12

+0

香港專業教育學院的代碼更新問題 – user2043267 2013-05-03 13:18:32