C# 一步创建项目并为项目添加图像
我试图通过制作一个电子商务网站来学习ASP.net。我正在尝试设置创建项目的功能,并通过文件上载将图像分配给正在创建的项目 我设法实现了多文件上传,但只上传到content/Images文件夹。我不知道如何将其与项目的创建相结合,以便您可以在项目的创建过程中为项目指定多个图像 公平地说,我不知道从这里到哪里去,我将感谢任何帮助 项目模型类:数据库中存储每个项目的表。从具有1对多关系的Images表中引用C# 一步创建项目并为项目添加图像,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我试图通过制作一个电子商务网站来学习ASP.net。我正在尝试设置创建项目的功能,并通过文件上载将图像分配给正在创建的项目 我设法实现了多文件上传,但只上传到content/Images文件夹。我不知道如何将其与项目的创建相结合,以便您可以在项目的创建过程中为项目指定多个图像 公平地说,我不知道从这里到哪里去,我将感谢任何帮助 项目模型类:数据库中存储每个项目的表。从具有1对多关系的Images表中引用 public class Item { [Key] [DatabaseG
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; }
}
创建项目视图
@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>
@model Project.Models.Item
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
项目
@LabelFor(model=>model.ItemName)
@EditorFor(model=>model.ItemName)
@Html.ValidationMessageFor(model=>model.ItemName)
@LabelFor(model=>model.ItemPrice)
@EditorFor(model=>model.ItemPrice)
@Html.ValidationMessageFor(model=>model.ItemPrice)
@LabelFor(model=>model.itemsdescription)
@EditorFor(model=>model.itemsdescription)
@Html.ValidationMessageFor(model=>model.ItemDescription)
@LabelFor(model=>model.ItemColour)
@EditorFor(model=>model.ItemColour)
@Html.ValidationMessageFor(model=>model.ItemColour)
@LabelFor(model=>model.ItemSize)
@EditorFor(model=>model.ItemSize)
@Html.ValidationMessageFor(model=>model.ItemSize)
}
@使用(Html.BeginForm(null,null,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
文件夹
}
@ActionLink(“返回列表”、“索引”)
看来你们很接近了
在将项目添加到数据库之前,只需将图像添加到项目中
因为您使用的是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();
我想这正是你想要的
另外,在模型构造函数中,通常我认为您希望初始化这些列表,以便在执行上述操作时不会得到null引用异常
public class Item
{
public Item()
{
this.Images = new List<Image>();
}
//...
}
公共类项目
{
公共项目()
{
this.Images=newlist();
}
//...
}
嗨,凯尔,谢谢你的回复。我遇到的最大问题是视图本身。目前我有一个上传按钮和一个创建按钮。当然我只需要一个按钮,将上传和创建在同一个链接的过程?
//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>();
}
//...
}