Razor视图为每个循环编辑不同的记录

时间:2016-05-04 19:16:04

标签: c# asp.net-mvc razor foreach

我为我的公司创建了一个HelpDesk,其中我有一个知识库区域。然后知识库记录具有子知识库注释。用户可以在其中创建知识库文章并为其添加单独的注释。然后可以转到详细视图查看知识库文章或转到编辑视图来编辑文章。我在编辑视图中显示了各自的注释(在他们自己的editorFor字段中),但是如何将它们保存回表中呢?

目前我可以编辑单个笔记,但不会保存。我是否需要单独的视图来单独编辑注释?我宁愿不要这样做。

这是我的知识库类:

public class Knowledgebase
{
    public Guid KnowledgebaseId { get; set; }

    public Guid CategoryId { get; set; }

    public virtual Category Category { get; set; }

    public Guid CreatedById { get; set; }

    public virtual User CreatedBy { get; set; }

    public DateTime CreatedDate { get; set; }

    public string Description { get; set; }

    public Guid TypeId { get; set; }

    public virtual Role Type { get; set; }

    public string Note { get; set; }

    public virtual ICollection<KnowledgebaseNote> KnowledgebaseNotes { get; set; }

}

和我的KnowledgebaseNotes类:

public class KnowledgebaseNote
{
    public Guid KnowledgeBaseNoteID { get; set; }

    public Guid KnowledgeBaseID { get; set; }

    public virtual Knowledgebase Knowledgebase { get; set; }

    public string Note { get; set; }

    public string AttachmentName { get; set; }

    public Guid UserNoteId { get; set; }

    public virtual User UserNote { get; set; }

    public DateTime NoteDate { get; set; }

    public bool Delete { get; set; }
}

我当前的观点(我希望用户能够在此处编辑字段而无需单独查看):

@if (Model.KnowledgebaseNotes != null)
{
    foreach (var item in Model.KnowledgebaseNotes.OrderBy(m => m.NoteDate).Reverse())
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.UserNote.FullName)
            </td>
            <td>
                @Html.EditorFor(modelItem => item.Note)
            </td>
            <td>
                <div>
                    @if (item.AttachmentName != null)
                    {
                        string path = System.Configuration.ConfigurationManager.AppSettings["MediaFolder"] + "/Knowledgebase/" + Model.Description + "/" + item.AttachmentName;
                        if (System.IO.File.Exists(path))
                        {
                            <div class="hidden-sm hidden-xs">@Html.ActionLink(item.AttachmentName, "ViewAttachment", new { controller = "File", filePath = path, fileName = item.AttachmentName }, new { target = "_blank" })</div>
                            <div class="hidden-lg hidden-md"><a href="@Url.Action("ViewAttachment", "File", new { filePath = path, fileName = item.AttachmentName })"><span class="glyphicon glyphicon-paperclip"></span></a></div>
                        }
                    }
                </div>

            </td>

        </tr>
    }

和我的控制器代码:

public ActionResult EditTechKnowledgebase(EditKnowledgebase editknowledgebase)
    {
        var descriptionObj = db.Knowledgebases.Where(k => k.Description == editknowledgebase.Description).FirstOrDefault();
        string CurrentDescription = db.Knowledgebases.Where(k => k.KnowledgebaseId == editknowledgebase.KnowledgebaseId).FirstOrDefault().Description;

        if ((ModelState.IsValid) & ((descriptionObj == null) | (CurrentDescription == editknowledgebase.Description)))
        {
            Knowledgebase knowledgebase = new Knowledgebase();
            knowledgebase.KnowledgebaseId = editknowledgebase.KnowledgebaseId;
            knowledgebase.CategoryId = editknowledgebase.CategoryId;
            knowledgebase.CreatedById = editknowledgebase.CreatedById;
            knowledgebase.CreatedDate = editknowledgebase.CreatedDate;
            knowledgebase.TypeId = editknowledgebase.TypeId;
            if (System.IO.Directory.Exists(ConfigurationManager.AppSettings["MediaFolder"] + "\\" + "Knowledgebase" + "\\" + CurrentDescription) & (CurrentDescription != editknowledgebase.Description))
            {
                string folderName = new FileController().ChangeFolderName("Knowledgebase", CurrentDescription, editknowledgebase.Description);
            }
            knowledgebase.Description = editknowledgebase.Description;
            knowledgebase.Note = editknowledgebase.Note;
            knowledgebase.KnowledgebaseNotes = editknowledgebase.KnowledgebaseNotes;

            knowledgebases.Update(knowledgebase);
            knowledgebases.Commit();

            return RedirectToAction("DetailTechKnowledgebase", new { id = knowledgebase.KnowledgebaseId, SearchCreatedBy = editknowledgebase.SearchUser, SearchCategory = editknowledgebase.SearchCategory, 
                SearchType = editknowledgebase.SearchType, Contains = editknowledgebase.Contains });
        }
        else
        {
            if (descriptionObj != null)
            {
                ModelState.AddModelError("", "A Knowledgebase with the name '" + editknowledgebase.Description + "' already exists.");

            }

            ViewBag.Category = db.Categories.Where(c => c.Status == 1);
            ViewBag.Type = db.Roles;

            return View(editknowledgebase);
        }

    }

0 个答案:

没有答案