I am having a problem sending parent ID to child ID, even if I do, I want to display the child's data only of a particular parent. In my code, List is the parent, and Notes are the children. When I create a List, I have redirected to Notes Index Page (Different Controller) along with ID but in all lists, I can see the same notes. I am using TempData in NotesController to keep hold of that ID.
List Controller:
//Index
public ActionResult Index()
{
return View(db.Lists.ToList());
}
//Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ListViewModel lists)
{
if (ModelState.IsValid)
{
Lists list = new Lists();
list.CreationDate = DateTime.Now;
list.ListName = lists.ListName;
db.Lists.Add(list);
db.SaveChanges();
int? idFromView = list.Id;
return RedirectToAction("Index", "NotesInLists", new { id = idFromView });
}
return View(lists);
}
Notes Controller:
//Index
public ActionResult Index(int? id)
{
TempData["idFromView"] = id;
return View(db.NotesInLists.ToList());
}
//Create
public ActionResult CreateWithtext()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateWithtext(NoteTagViewModel notesInList)
{
if (ModelState.IsValid)
{
List<string> TagsList = notesInList.TagsList.Split(',').ToList();
NotesInList note = new NotesInList();
int tempDataId = (int)TempData["idFromView"];
note.CreationDate = DateTime.Now;
note.ListName = notesInList.ListName;
note.TextDescription = notesInList.TextDescription;
note.listID = tempDataId;
db.NotesInLists.Add(note);
db.SaveChanges();
//saving tags
foreach (var item in TagsList)
{
Tags tag = new Tags();
tag.CreationDate = DateTime.Now;
tag.TagName = item;
tag.Note_Id = note.Id;
db.Tags.Add(tag);
}
db.SaveChanges();
return RedirectToAction("Index", new { id = tempDataId });
}
return View(notesInList);
}
Here, in this NotesController, I am also saving tags and it is working fine, but the main issue is with List. Also using ViewModels but that is of no concern to me for now. If I try accessing List using
Lists list = new List();
I still am not able to check and compare that ID with that List ID, it throws an exception.
List Model:
namespace NoteBlocks.Models
{
public class Lists
{
public int Id { get; set; }
[Required]
[Display(Name = "List Name")]
public string ListName { get; set; }
[Display(Name = "Creation Date")]
public DateTime? CreationDate { get; set; }
[Display(Name = "Last Updated")]
public DateTime? UpdateDate { get; set; }
}
}
List ViewModel:
namespace NoteBlocks.ViewModels
{
public class ListViewModel
{
public int Id { get; set; }
[Required]
[Display(Name = "List Name")]
public string ListName { get; set; }
[Display(Name = "Creation Date")]
public DateTime? CreationDate { get; set; }
[Display(Name = "Last Updated")]
public DateTime? UpdateDate { get; set; }
}
}
Notes Model:
namespace NoteBlocks.Models
{
public class NotesInList
{
public int Id { get; set; }
[Required]
[Display(Name = "List Name")]
public string ListName { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Creation Date")]
public DateTime? CreationDate { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Last Updated")]
public DateTime? UpdateDate { get; set; }
public string customFile { get; set; }
[Display(Name = "Enter Note Content")]
public string TextDescription { get; set; }
public Lists List { get; set; }
public int listID { get; set; }
}
}
Notes ViewModel:
namespace NoteBlocks.Models
{
public class NoteTagViewModel
{
public int NoteId { get; set; }
[Required]
[Display(Name = "List Name")]
public string ListName { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Creation Date")]
public DateTime? CreationDate { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Last Updated")]
public DateTime? UpdateDate { get; set; }
public string customFile { get; set; }
[Display(Name = "Enter Note Content")]
public string TextDescription { get; set; }
//multiple tags
public string TagsList { get; set; }
public Lists List { get; set; }
public int ListId { get; set; }
}
}
Created a foreign key but it is not working.
HTML - List Index
@model IEnumerable<NoteBlocks.Models.Lists>
@{
ViewBag.Title = "List";
//ViewBag.Id = model.Lists.Id;
}
<h2>List</h2>
<p>
@Html.ActionLink(" Create New List", "Create")
</p>
<table >
<tr>
<th>
@Html.DisplayNameFor(model => model.ListName)
</th>
<th>
@Html.DisplayNameFor(model => model.CreationDate)
</th>
<th>
@Html.DisplayNameFor(model => model.UpdateDate)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
//using ActionLink to open list of notes for that particular List
//and passing particular List ID from here if already created
@Html.ActionLink(item.ListName, "Index", "NotesInLists", new { id = item.Id }, null)
@*@TempData.Peek("tempDataId")*@
</td>
<td>
@Html.DisplayFor(modelItem => item.CreationDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.UpdateDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
HTML - List Create
@model NoteBlocks.Models.Lists
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div >
<h4>List</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div >
@Html.LabelFor(model => model.ListName, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.ListName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ListName, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.CreationDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.CreationDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreationDate, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.UpdateDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.UpdateDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UpdateDate, "", new { @class = "text-danger" })
</div>
</div>
<div >
<div >
<input type="submit" value="Create" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
HTML - Notes Index
@model IEnumerable<NoteBlocks.Models.NotesInList>
@{
ViewBag.Title = "List Notes";
}
<h2>List Notes</h2>
<p id="buttonsInPTag">
<button type="button" id="addButton1"><span >@Html.ActionLink(" Create Textual Note", "CreateWithtext")</span></button>
<button type="button" id="addButton2"><span >@Html.ActionLink(" Create Note from Document", "CreateWithDoc")</span></button>
<button type="button" id="addButton3"><span >@Html.ActionLink(" Create Image Note", "CreateWithImage")</span></button>
<button type="button" id="addButton4"><span >@Html.ActionLink(" Create Audio / Video Note", "CreateWithMedia")</span></button>
</p>
<table >
<tr>
<th>
@Html.DisplayNameFor(model => model.ListName)
</th>
<th>
@Html.DisplayNameFor(model => model.CreationDate)
</th>
<th>
@Html.DisplayNameFor(model => model.UpdateDate)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ListName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreationDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.UpdateDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
HTML - Notes CreateWithText
@model NoteBlocks.Models.NoteTagViewModel
@{
ViewBag.Title = "Create Note with Text";
}
<h2>Create Note with Text</h2>
@using (Html.BeginForm("CreateWithText", "NotesInLists", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div >
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div >
@Html.LabelFor(model => model.ListName, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.ListName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ListName, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.TextDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
<textarea cols="50" rows="12" class=form-control id="TextDescription" name="TextDescription"></textarea>
@Html.ValidationMessageFor(model => model.TextDescription, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.Label("Tags", htmlAttributes: new { @class = "control-label col-md-2" })
<div >
<input type="text" id="tagsField" name="tagsField" class=form-control data-role="tagsinput" />
<input type="hidden" name="TagsList" id="TagsList" />
</div>
</div>
<div >
<div >
<input type="submit" value="Create" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section scripts
{
<script>
$(document.body).on('focusout', '.bootstrap-tagsinput input', () => {
let array = $('#tagsField').tagsinput('items');
$("#TagsList").val(array);
})
</script>
}
I am stuck to this point. Please guide. p.s. using code first approach.
CodePudding user response:
There is no filter applied to the query that gets notes in your Index action of notes controller.
I think adding where statement;
.Where(x=>x.listID == id)
will solve your problem.
//Index
public ActionResult Index(int? id)
{
TempData["idFromView"] = id;
return View(db.NotesInLists/*.Where(x=>x.listID == id)*/.ToList());
}