[英]How to add a Comment to an entity in a one-to-many relationship
這是愚蠢的情況:
我有一個Device
類
[Key]
public int Id { get; set; }
[MaxLength(50)]
public String Name { get; set; }
[Required]
public Category Category { get; set; }
[Required]
public Manufactor Manufactor { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Status> Status { get; set; }
和A類Comment
public int ID { get; set; }
public string Content { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateCreated { get; set; }
public string Author { get; set; }
public virtual Device Device { get; set; }
如您所見,設備的實體可以有很多注釋,但是一個注釋只有一個設備。
在我的控制器中,我有以下兩個操作:
public ActionResult AddComment(int id)
{
Device device = db.Devices.Include(x => x.Comments).Where(dev => dev.Id == id).FirstOrDefault();
if (device == null)
{
return HttpNotFound();
}
return View(device);
}
[HttpPost, ActionName("CommentAdded")]
public ActionResult CommentAdded(int id)
{
Device device = db.Devices.Find(id);
db.Entry(device).State = EntityState.Modified;
db.Devices.AddOrUpdate(device);
foreach(var comment in device.Comments)
{
db.Comments.AddOrUpdate(comment);
}
db.SaveChanges();
return View("Index");
}
到目前為止,一切似乎都是直截了當的。 但是突然之間,我不知道如何創建一個視圖,在其中可以執行以下三件事:
我可以輕松實現1和2,但是我不知道如何添加其他評論然后提交表單。
AddComment.cshtml:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Device</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DisplayFor(model => model.Name)
</div>
<div class="col-md-6">
<br/>
<div class="row">
@Html.EditorFor(model => model.Comments)
</div>
@foreach (var comment in Model.Comments)
{
<div class="row">
<div class="col-md-4">
<strong>
@Html.DisplayFor(model => comment.Author)
</strong>
</div>
<div class="col-md-4">
<strong>
@Html.DisplayFor(model => comment.DateCreated)
</strong>
</div>
<div class="col-md-4">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>
@Html.DisplayFor(model => comment.Content)
</p>
</div>
</div>
}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
當前的問題是:我需要在視圖(位置控制器)中進行哪些更改才能向所選設備添加其他注釋?
遵循這些原則。 只需創建一個html表單並將表單字段映射到ajax函數。 然后在提交表單時調用該函數。
希望有幫助。
[HttpPost]
public ActionResult AddComment(int id,Comment comment)
{
Device device = db.Devices.Find(id);
comment.DateCreated = DateTime.Now;
Device.Comment.Add(comment);
db.Entry(device).State = EntityState.Modified;
db.saveChanges();
return Json("Success");
}
$.ajax({
type: "POST",
url: "/AddComment",
data: { Id: '@Html.IdFor(m => m.Id)',
Comment: { Content: 'data', Author: 'Author'} },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data == "Success") {
$('#COMMENTSDIV').append(" YOUR COMMENTS MARKUP HERE ");
}
}
});
您只需要在表單中包裝添加注釋功能,然后通過相應的操作將其發布回服務器即可。 如果需要,該操作可以返回整個視圖,並添加新的注釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.