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