嗨
我在下面列出了以下型号和控制器
我尝试通过foreignkey在视图创建中引入下拉列表 "标题"但我无法在创建/保存中获得错误
请帮助代码 - (我是初学者)
imprumutsController.cs
....
// GET: imprumuts/Create
public ActionResult Create()
{
ViewBag.idcarti_stoc = new SelectList(db.carti_stoc, "idcarti", "idcarti" );
ViewBag.idcititor = new SelectList(db.citiors, "Idcititor", "Numele");
// ViewBag.idcititor = new SelectList(db.cartis, "Idcarti", "Titlu");
ViewBag.idcarti = new SelectList(db.cartis, "Idcarti", "Titlu");
return View();
}
// POST: imprumuts/Create
[enter image description here][1]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Idimprumut,idcarti_stoc,idcititor,data_imprumut,durata,data_restituire")] imprumut imprumut)
{
if (ModelState.IsValid)
{
db.imprumuts.Add(imprumut);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.idcarti_stoc = new SelectList(db.carti_stoc, "idcarti_stoc", "idcarti_stoc", imprumut.carti_stoc.carti.Titlu);
// ViewBag.idcart_stoc = new SelectList(db.carti_stoc, "idcarti_stoc", "idcarti_stoc", imprumut.idcarti_stoc);
// ViewBag.idcititor = new SelectList(db.citiors, "Idcititor", "idcititor", imprumut.idcititor);
return View(imprumut);
}
imprumuts - Create.cshtml
...
@* @Html.LabelFor(model => model.idcarti_stoc, "idcarti_stoc", htmlAttributes: new { @class = "control-label col-md-2" })
*@
@Html.LabelFor(model => model.idcarti_stoc, "Titlu", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("idcarti", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.idcarti_stoc, "", new { @class = "text-danger" })
</div>
</div>
namespace carti_v2.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class imprumut
{
public int Idimprumut { get; set; }
public int idcarti_stoc { get; set; }
public int idcititor { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public System.DateTime data_imprumut { get; set; }
public int durata { get; set; }
public Nullable<System.DateTime> data_restituire { get; set; }
public virtual carti_stoc carti_stoc { get; set; }
public virtual citior citior { get; set; }
}
}
namespace carti_v2.Models
{
using System;
using System.Collections.Generic;
public partial class carti_stoc
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public carti_stoc()
{
this.imprumuts = new HashSet<imprumut>();
}
public int idcarti_stoc { get; set; }
public int idcarti { get; set; }
public bool disponibil { get; set; }
public virtual carti carti { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<imprumut> imprumuts { get; set; }
}
}
*****
namespace carti_v2.Models
{
using System;
using System.Collections.Generic;
public partial class carti
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public carti()
{
this.carti_stoc = new HashSet<carti_stoc>();
}
public int idcarti { get; set; }
public string Autor { get; set; }
public string Titlu { get; set; }
public string Domeniu { get; set; }
public string ISBN { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<carti_stoc> carti_stoc { get; set; }
答案 0 :(得分:0)
请记住,如果某个字段上有外键,那么该字段需要在主字段中具有匹配值。
你需要放置单独的SaveChanges()以便关系一致。像FK_imprumut_carti_stoc失败意味着一些carti_stoc imprumut字段不会出现在imprumut中
你可以这样做:
public ActionResult Index63()
{
//use your dbcontext
using (BreazEntities19 entity = new BreazEntities19())
{
//entity.SaveChanges();
imprumut i = new imprumut
{
carti_stoc = 1, //THIS Needs to be unique
data_imprumut = DateTime.Parse("12/12/12"),
data_restituire = DateTime.Parse("12/12/12"),
durata = 1,
idcarti_stoc = 1,
idcititor = 1
};
entity.imprumuts.Add(i);
entity.SaveChanges();
carti_stoc cs = new carti_stoc
{
carti = 1, //THIS Needs to be unique
disponibil = true,
idcarti = 1,
impromut = i.carti_stoc
};
entity.carti_stoc.Add(cs);
entity.SaveChanges();
carti c = new carti { Autor = "ap1", carti_stoc = cs.carti, Domeniu = "ap", ISBN = "ap", Titlu = "ap" };
entity.cartis.Add(c);
entity.SaveChanges();
}
您可以在事务中包装所有内容:
using (TransactionScope scope = new TransactionScope())
{
//Put your work in here with multiple save changes
using (myDbcontext context = new myDbcontext()){
...
context.SaveChanges();
}
scope.Complete();
}
答案 1 :(得分:0)
这是您的视图模型,使用您的命名空间:
namespace Testy20161006.Models
{
public partial class carti
{
public string selectedItem {get; set;}
}
}
这是你的控制器:
{
[HttpPost]
public ActionResult Index63(carti model)
{
//put a breakpoint here to see selected idcarte_stoc, as you want
return View();
}
public ActionResult Index63()
{
//use your dbcontext name, be aware that you are not explicitely disposing of the obj
BreazEntities19 entity = new BreazEntities19();
SelectList selectList = new SelectList(entity.cartis, "carti_stoc", "Titlu");
ViewBag.idcarti = selectList;
return View();
}
以下是您的观点:
@model Testy20161006.Models.carti
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index63</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.DropDownListFor(m => m.selectedItem,
new SelectList(ViewBag.idcarti, "Value", "Text"),
new { @class = "form-control" })
<input type="submit" value="click" />
}
</body>
</html>
答案 2 :(得分:0)
这里是带有数据库结构的图像-diagram enter image description here
最终我认为应该通过idcarti和iscarti_stoc之间的链接下载级联。 但我不知道如何从视图中检索选择的idcards,使控制器成为第二个选择......