0

I have created a HelpDesk for my company and in it I have a knowledgebase area. A knowledgebase record then has a child knowledgebaseNotes. In it a user can create knowledgebase articles and add separate notes to them. Then later can go to the detail view to view the knowledgebase article or go to the edit view to edit the article. I have the individual notes showing up on the edit view (in their own editorFor fields) but how do I save them back to the table?

Currently I can edit the individual notes but they won't save. Would I need a separate view to edit the notes individually? I would prefer not to have to do that.

Here is my knowledgebase class:

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; }

}

and my KnowledgebaseNotes class:

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; }
}

My current view (I want users to be able to edit the fields here without going to a separate view):

@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>
    }

and my controller code:

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);
        }

    }
djblois
  • 963
  • 1
  • 17
  • 52
  • That is a lot of code for an Action in a controller. I would suggest, place an edit button in each row of your table, and if that button is clicked, open a partial view where you display the details to be edited. – monstertjie_za May 04 '16 at 19:23
  • @monstertjie_za, I just want to make sure I understand you completely. You are suggesting an edit button (that will show on each row) and when clicked will open a partial view just for that record (inline) for the user to edit then save? – djblois May 04 '16 at 19:27
  • Yes, unless I misunderstood your question. A better approach may even be to make the entire row clickable, than have a button. If I misunderstood you, please correct me, and I will try and help where possible – monstertjie_za May 04 '16 at 20:17
  • You cannot use a `foreach` loop - you need a `for` loop of an `EditorTemplate` for `KnowledgebaseNotes` –  May 04 '16 at 22:22

0 Answers0