在mvc中处理图像和crud

1001 观看

2回复

41 作者的声誉

有没有人知道如何在mvc asp.net中使用CRUD时上传,以便我的图像将被上传到我的标题内,无论是在自己的div中还是在标题内的特定div中,现在我可以将图片上传到身体内容,但我不知道如何将其上传到标题中的logoelement。

这是代码,所以你可以看到我希望它显示在哪里@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
}
作者: user2043267 的来源 发布者: 2013 年 5 月 3 日

回应 (2)


0

4430 作者的声誉

我想你希望它显示在页面的左上角。只要把你的形象<div id="title">

作者: İsmet Alkan 发布者: 03.05.2013 12:46

0

35 作者的声誉

您需要在此操作中返回图片URL

[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);
//Return picture URL here
    }
作者: user3452580 发布者: 12.07.2019 09:41
32x32