How to show master-detail data on an ASP.NET mvc page?

I already read a few SO questions on this topic, but to be honest, most of them were too complicated for me. I am very new to ASP.NET mvc.

I have an example ASP.NET mvc 4 application that I created by following along with (and straying a bit) from the Movie database tutorial. It has built-in account bits, Entity Framework (which changed the pain at any time), plus 2 models that I built based on the models from the tutorial:

1) Error

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; namespace MasterDetailPractice.Models { public class Bug { public int BugID { get; set; } public string BugTitle { get; set; } public DateTime BugDate { get; set; } public string BugStatus { get; set; } [Column(TypeName = "ntext")] [MaxLength] public string BugDescription { get; set; } } public class BugDBContext : DbContext { public DbSet<Bug> Bugs { get; set; } public DbSet<Comment> Comments { get; set; } } } 

2) Comment

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; namespace MasterDetailPractice.Models { public class Comment { public int CommentID { get; set; } public int BugID { get; set; } public int UserID { get; set; } public DateTime CommentDate { get; set; } [Column(TypeName = "ntext")] [MaxLength] public string CommentText { get; set; } } } 

When I launch my application, I can go to / Project and get the standard index view using the Add link, where I can add an error. After adding, I see the usual links "Edit / Details / Delete".

When I launch my application, I can also go to / Comment and get a standard index view using the Add link, where I can add a comment. After adding, I see the usual links "Edit / Details / Delete".

Until this moment, I'm fine. CRUD shapes work, they just don't work together.

PROBLEM:

Currently, in order to make the comment applicable to the error, I have to actually enter BugID in the form / Comment / Create. And then comments are all available only on / Comment / route.

Instead, I need the following:

  • The Add Comment form should automatically know what BugID to save if the user does not have to enter it.
  • Detailed description of the data: the view / comment / index should appear at the bottom of the page / Bug / Edit and / or Bug / Details and show only comments related to the current error.
  • The Add Comment link should only appear from / Bug / Edit or / Bug / Details, so comments are never added without reference to the error.

Surprisingly, I could not figure it out myself, spending 3 days so that every Google result and SO message could be found on this topic. However, I am here, hoping to find out the simplest possible implementation of this.

Should I post more code (e.g. Controllers or Views) so that this question answers properly?

We are looking forward to the slow train starting to leave the station ...

+4
source share
1 answer

Ok, you need to do a few things.

First, create a new action method in your CommentController that looks like this.

 public ActionResult Index(int bugId) { // Your logic to fetch all comments by BugID through EntityFramework or whatever return View(data); } 

Now on the Bug / Edit.cshtml or Bug / Details.cshtml pages, add the following line to make these actions inline.

 @Html.RenderAction("Index", "Comment", new { @bugId = Model.BugID } 

In this case, you should return the BugModel back to your Bug / Edit.cshtml or Bug / Details.cshtml anyway as your model.

This should show you the form you need with the BugID from the passed model.

For your last question, just put the Add Comment link in your Comment / Index.cshtml comment, as it will appear anyway in the context of the error. You may have to wrap this around a form that is sent to your comment. Controller.

Here is a useful link to working with forms in ASP.NET 4.

http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-helpers,-forms-and-validation

0
source

Source: https://habr.com/ru/post/1496467/


All Articles