我正在使用ASP.NET MVC 5和实体框架。
我有以下三个模型类:
public class SubCat
{
public int Id { get; set; }
public string Description { get; set; }
[ForeignKey("Category")]
public int CategoryId { get; set; }
public Category Category { get; set; }
}
public class Item
{
public Item()
{
Images = new HashSet<Image>();
}
public int Id { get; set; }
public string Description { get; set; }
[ForeignKey("SubCat")]
public int? SubCatId { get; set; }
public SubCat SubCat { get; set; }
//public byte? Image { get; set; }
[ForeignKey("Images")]
public int? ImageId { get; set; }
public ICollection<Image> Images { get; set; }
public int ItemPoints { get; set; }
public int? PointsValue { get; set; }
public string Status { get; set; }
[Display(Name = "Delivery")]
public string DeliveryChoice { get; set; }
public bool? Approved { get; set; }
public bool? Bartered { get; set; }
}
public class Image
{
public int Id { get; set; }
[DisplayName("Upload File")]
public string Picture { get; set; }
[NotMapped]
public HttpPostedFileBase ImageFile { get; set; }
[ForeignKey("Item")]
public int? ItemId { get; set; }
public Item Item { get; set; }
}
添加新项目时,需要给它一个子类别ID,以便在视图中显示它,这是添加新项目的方法:
表格:
@using (Html.BeginForm("ItemSubmit", "Orders", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(m => m.Item.Description, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.Item.Description, new { @class = "form-control" })
</div>
<br />
<div class="form-group">
<div class="col-md-6">
@Html.DropDownListFor(m => m.Category.Id, new SelectList(Model.Categories, "Id", "Description"), "Select Category", new { @class = "form-control", @id = "cats" })
</div>
<div class="col-md-6">
<select id="subs" disabled class="dropdown form-control" name="subCategory">
<option>Select Category First</option>
</select>
</div>
<br />
<br />
<br />
<div class="form-group">
@Html.LabelFor(m => m.Item.Status, new { @class = "col-md-2 control-label" })
@Html.DropDownListFor(m => m.Item.Status, new SelectList(Model.Items, "Status", "Status"), "", new { @class = "form-control" })
</div>
<br />
<div class="form-group">
@Html.LabelFor(m => m.Item.DeliveryChoice, new { @class = "col-md-2 control-label" })
@Html.DropDownListFor(m => m.Item.DeliveryChoice, new SelectList(Model.Items, "DeliveryChoice", "DeliveryChoice"), "", new { @class = "form-control" })
</div>
<div class="form-group">
<div class="col-md-1"></div>
<h3><span class="label label-primary">How many points would you suggest for your Item?</span></h3>
<div class="col-md-3"></div>
@Html.TextBoxFor(m => m.Item.ItemPoints, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Image.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
<input type="file" name="ImageFile" required>
</div>
<br />
<br />
<div class="row">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
}
和控制器:
[HttpPost]
public ActionResult ItemSubmit(Item item, int subCategory, Image imageModel, Order order, ApplicationUser user)
{
if (ModelState.IsValid)
{
var sub = _context.SubCats.SingleOrDefault(m => m.Id == subCategory);
//Item
item.SubCatId = sub.Id;
//Image
string fileName = Path.GetFileNameWithoutExtension(imageModel.ImageFile.FileName);
string extension = Path.GetExtension(imageModel.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
imageModel.Picture = "~/Image/" + fileName;
fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
imageModel.ImageFile.SaveAs(fileName);
//Order
order.OrderDate = DateTime.Now;
order.TransactionType = "Item Submit";
order.UserId = User.Identity.GetUserId();
order.Approved = false;
order.ItemId = item.Id;
_context.Items.Add(item);
_context.Images.Add(imageModel);
_context.Orders.Add(order);
_context.SaveChanges();
}
return RedirectToAction("Index","Home");
}
但是当调用_context.SaveChanges()时,ef倾向于保存该项目的两个副本!一个带有subcatid的子项,一个不带subcatid的子项,有趣的是,图像采用了没有subcatid的项的itemid,所以当我尝试通过图像在视图上显示该项目时,它不显示,因为显示的项目必须与选择的子猫有关。
这是我在视图中使用的jQuery:
$(document).ready(function () {
$(".mybtn").on('click', function () {
var id = $(this).attr('data-id');
$("#images").html('');
$.get("/Items/GetSubById", { ID: id }, function (data) {
$("#sub-categories-btns").html(``);
for (var i = 0; i < data.length; i++) {
$("#sub-categories-btns").append(`
<button id="button" type="button" data-id="`+ data[i].Id + `" class="btn btn-success my-subcategory-btn">
`+ data[i].Description +`
</button>
`);
}
});
});
$("#sub-categories-btns").delegate(".my-subcategory-btn", 'click', function () {
var subId = $(this).attr('data-id');
$("#images").html('');
$.get("/Items/GetImgaesBySubCategoryId", { ID: subId }, function (data) {
for (var i = 0; i < data.length; i++) {
$("#images").append(`
<div class="form-group col-md-4">
<button data-id="`+ data[i].ItemId +`" name="item" class="ItemInfo img-btn"><img src="`+ data[i].Picture +`" width="200" height="200" /></button>
</div>
`);
}
});
});
$("#images").delegate('.img-btn', 'click', function () {
var imgId = $(this).attr('data-id');
window.location.href = "/Items/Buy?item=" + imgId;
});
});
这是我发送到视图的json:
public JsonResult GetSubById(int? ID)
{
return Json(_context.SubCats.Where(p => p.CategoryId == ID), JsonRequestBehavior.AllowGet);
}
public JsonResult GetImgaesBySubCategoryId(int? ID)
{
var data = _context.Images.Where(m => m.Item.SubCatId == ID).ToList();
data.ForEach(img => img.Picture = img.Picture.Replace("~", ""));
return Json(data, JsonRequestBehavior.AllowGet);
}
如何阻止它复制?
答案 0 :(得分:0)
我实际上是在不久前找到解决方案的,但是我忘了在这里发布!
我不知道如何解释它,但是在创建图像时,它用新项目填充了项目导航属性,该新项目具有与表单发送的项目相同的信息! 所以我只是在ItemSubmit Controller中上传图像代码之前添加了这两行
imageModel.Item = null;
imageModel.ItemId = item.Id;
我在从视图模型发送的模型中也遇到了问题,所以我评论了if(ModelState.IsValid)
希望这对其他人有帮助