我创建了一个应用程序来引导用户完成一些数据收集过程。他有一个实体'客户',在不同的视图中添加不同的数据集。
最后,我想向他展示一个概述,他也可以添加评论。注释作为由外键连接的子实体保存在另一个表中。 有没有办法从这种形式的viewmodel创建一个子实体?
这是我的父实体客户。
public class Customer
{
[Key]
public int Code { get; set; }
[Required]
[Display(Name = "FamilyName", ResourceType = typeof(TextResourceFile))]
public string FamilyName { get; set; }
[Required]
[Display(Name = "Name", ResourceType = typeof(TextResourceFile))]
public string Name{ get; set; }
[Display(Name = "PostalCode", ResourceType = typeof(TextResourceFile))]
public string PostalCode { get; set; }
[Display(Name = "City", ResourceType = typeof(TextResourceFile))]
public string City { get; set; }
[Display(Name = "StreetAndNumber", ResourceType = typeof(TextResourceFile))]
public string StreetAndNumber { get; set; }
[DataType(DataType.Date)]
[Display(Name = "BirthDate", ResourceType = typeof(TextResourceFile))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? BirthDate { get; set; }
[DataType(DataType.Date)]
[Display(Name = "CreationDate", ResourceType = typeof(TextResourceFile))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? CreationDate { get; set; }
[DataType(DataType.Date)]
[Display(Name = "ModificationDate", ResourceType = typeof(TextResourceFile))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime ModificationDate { get; set; }
public virtual ICollection<CustomerNote> CustomerNotes { get; set; }
public virtual ICollection<somethingElse> somethingElse{ get; set; }
public virtual ICollection<somethingElse> somethingElse{ get; set; }
public virtual ICollection<somethingElse> somethingElse{ get; set; }
public virtual ICollection<somethingElse> somethingElse{ get; set; }
public virtual ICollection<somethingElse> somethingElse{ get; set; }
public virtual ICollection<somethingElse> somethingElse{ get; set; }
}
这是我的孩子CustomerNote
public class CustomerNote
{
[Key]
public int Code { get; set; }
public int CustomerCode { get; set; }
[Display(Name = "Anamnesis", ResourceType = typeof(TextResourceFile))]
public string Anamnesis { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ModificationDate { get; set; }
}
这是我最初的Create()Methode,其中包含所有必要的数据:
public ActionResult Create(int? code)
{
if (code == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var customer = db.Customer.FirstOrDefault(cu => cu.Code == code);
if (customer == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.Customercode = customer.Code;
var customNote = new CustomerNote();
customNote.CustomerCode = customer.Code;
customNote.Anamnesis = string.Empty;
customer.CustomerNotes.Add(customNote);
return View(customer);
}
这是我的Post方法,我想保存评论:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(/*[Bind(Include = "CustomerNote")] */Customer customer)
{
if (customer != null && customer.CustomerNotes != null)
{
var customerNote = new CustomerNote();
customerNote.CreationDate = DateTime.Now;
customerNote.ModificationDate = DateTime.Now;
customerNote.CustomerCode = customer.Code;
customerNote.Anamnesis = customer.CustomerNotes.OrderByDescending(x => x.CreationDate).FirstOrDefault().Anamnesis;
db.CustomerNote.Add(customerNote);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View(customer);
}
在我看来,我显示的数据如下,但当然不起作用。我该如何改变呢?
<br />
<h3>@Html.LabelFor(model => model.CustomerNotes.OrderByDescending(x => x.CreationDate).FirstOrDefault().Anamnesis)</h3>
<div>
@Html.TextBoxFor(model => model.CustomerNotes.OrderByDescending(x => x.CreationDate).FirstOrDefault(), new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.CustomerNotes.OrderByDescending(x => x.CreationDate).FirstOrDefault().Anamnesis, "", new {@class = "text-danger"})
</div>
我现在添加了一个viewmodel:
public class CustomerNoteIndexData
{
public int CustomerCode { get; set; }
public CustomerNote CustomerNote { get; set; }
public Customer Customer { get; set; }
public otherInformationLists otherInformationLists { get; set; }
public otherInformationLists otherInformationLists { get; set; }
public otherInformationLists otherInformationLists { get; set; }
public otherInformationLists otherInformationLists { get; set; }
public otherInformationLists otherInformationLists { get; set; }
public otherInformationLists otherInformationLists { get; set; }
}
他们不是列表,但显示每个实体的最新条目。
现在不知何故,CustomerCode保持空白。