繁体   English   中英

一步创建商品并为商品添加图片

[英]Create Item and add images for Item in one step

我正在尝试通过建立一个电子商务站点来学习ASP.net。 我正在尝试建立创建项目并将图像分配给通过文件上传创建的项目的功能。

我设法使多个文件上传正常工作,但仅上传到content / Images文件夹。 我想不通将其与项目创建结合使用,因此您可以在项目创建过程中为项目分配多个图像。

可以公平地说,我不知道从这里去哪里,不胜感激。

物料模型类:数据库中用于存储每个物料的表。 从图像表中以一对多关系引用。

 public class Item
 {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ItemId { get; set; }
    public int CategoryId { get; set; }
    public int DesignerId { get; set; }
    public int ImageId { get; set; }
    [Required]
    [MaxLength(250)]
    public string ItemName { get; set; }
    [Required]
    [Range(0,9999)]
    public decimal ItemPrice { get; set; }
    [MaxLength(1000)]
    public string ItemDescription { get; set; }
    [Range(4,22)]
    public int ItemSize { get; set; }

    [Column("CategoryId")]
    public virtual List<Category> Category { get; set; }
    public virtual List<OrderDetail> OrderDetails { get; set; }
    public virtual List<Image> Images { get; set; }
}

图像模型类:将每个图像的URL存储在站点的内容目录中。 每个项目可以有很多图像。

public class Image
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ImageId { get; set; }
    [Required]
    public string ImageURL { get; set; }
    [Required]
    public string ItemId { get; set; }

    //Files Being Uploaded by the User
    public IEnumerable<HttpPostedFileBase> Files { get; set; }

    [Column("ItemId")]
    public virtual List<Item> Item { get; set; }
}

店铺经理控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Item item,HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {

            //The below successfully saves the file to the content folder when separated into the Index Action.
            foreach (var f in item.Files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(f.FileName);
                    var path =   Path.Combine(Server.MapPath("~/Content/ItemImages/"+item), fileName);
                    file.SaveAs(path);
                }
            }

    // The below also works when I dont have the Above in the Action.
            db.Items.Add(item);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(item);
    }

创建项目视图

    @model Project.Models.Item

    @{
       ViewBag.Title = "Create";
     }

    <h2>Create</h2>

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

    <fieldset>
        <legend>Item</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ItemName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ItemName)
            @Html.ValidationMessageFor(model => model.ItemName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ItemPrice)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ItemPrice)
            @Html.ValidationMessageFor(model => model.ItemPrice)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ItemDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ItemDescription)
            @Html.ValidationMessageFor(model => model.ItemDescription)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ItemColour)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ItemColour)
            @Html.ValidationMessageFor(model => model.ItemColour)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ItemSize)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ItemSize)
            @Html.ValidationMessageFor(model => model.ItemSize)
        </div>

        <p>
           <input type="submit" value="Create" />
        </p>
</fieldset>
}

    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div>
        <table>
            <tr>
                <td>Files</td>
                <td><input type="file" name="Files" id="Files" multiple/></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="Upload" /></td>
            </tr>
        </table>
        </div> 
    }

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

所以看起来您已经很接近了。

您只需要将图像添加到项目中,然后再将其添加到数据库中。

由于您使用的是EF,因此应该与此类似

 //in your action

 //add the images to the item
 item.Images.Add(new Image { ImageUrl = ... });

 //you should be able to just insert the whole entity graph here
 db.Items.Add(item);
 db.SaveChanges();

我认为您正在寻找类似的东西。

同样在您的模型构造函数中,我通常希望对这些列表进行初始化,以免在执行上述操作时不会得到空引用错误

public class Item 
{
    public Item()
    {
        this.Images = new List<Image>();
    }
    //...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM