我有一个像这样的 ContactModel:
public class ContactModel
{
public virtual int ID { get; set; }
public virtual string LastName { get; set; }
public virtual ICollection<Note> Notes { get; set; }
}
子集合Note是这样定义的:
public class Note
{
public virtual int ID { get; set; }
public virtual ContactModel ContactModel { get; set; }
public virtual string NotesValue { get; set; }
}
我添加了一个 Controller ,它接受 ContactModel 并为其创建索引、编辑和创建 View 。
例如我稍微修改了联系人的索引 View 以获取部分 View :
@foreach (var item in Model)
{
@Html.Partial("_ContactView", item)
}
在部分 _ContactView 中我有这个:
<span class="names"><a href="????">@Model.LastName</a></span>
现在这里是我卡住的地方,我想为 Note 类创建一个 EditView 并从上面的 URL 指向这个 View 。这意味着当用户点击姓氏时,它应该打开注释的编辑 View ,以便为这个人添加额外的注释。
我如何实现这一目标?
最佳答案
您可以创建一个专用于处理笔记的新 Controller 。像这样为其创建 URL:
<span class="names"><a href="@Html.Action("Edit", "Notes", new { ContactModelId = @Model.ID })">@Model.LastName</a></span>
在新的 NotesController
中,根据通过 URL 传递的 ContactModel
的 ID 加载您的笔记集合:
public class NotesController : Controller {
public ActionResult Edit(int contactModelId) {
var result = Repository.ContactModel.Where(x => x.ContactModelId).Select(x => x.Notes);
return View(result);
}
}
根据需要向 NotesController
类添加用于添加/更新注释的代码。
关于c# - 如何编辑模型的 ChildCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9851038/