Avatar of Peter Groves
Peter GrovesFlag for Canada

asked on 

Display multi-table in view

If I display one table by itself it show ok. But I'm trying to figure out the syntax for
a mutiple table view.

public async Task<IActionResult> Index()
        {
            // return View(await _context.AnalSubTasks.ToListAsync());
            var getanalsub = (IEnumerable<AnalSubTask>)_context.AnalSubTasks.FromSqlRaw("spGetanalsub null").ToList();
                return View(getanalsub);
        } 

Open in new window

@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Ident)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.date_created)
            </td>

Open in new window

But if I use a multitable I can't figure out the syntax.

@model OASIS_MVC.View.ViewModel.mainviewmodelV


    <tbody>
        @foreach (var item in Model.SubTasks)
        {
            <tr>
                <td>
                   
                    @Html.DisplayFor(modelItem => Model.SubTasks.Ident)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => Model.SubTasks.date_created)
                </td> 

Open in new window


The controller part .
       public IActionResult Edit(int? id)
        {
            var tables = new mainviewmodelV
            {
                getanalmainV = (IEnumerable<Mainview>)_db3.mainviewmodelV.FromSqlRaw("spGetanalmain {0},{1}", null,id).ToList(),
                SubTasks = (IEnumerable<AnalSubTask>)_db3.AnalSubTasks.FromSqlRaw("spGetanalsub null").ToList()
        };
            return View(tables);
   
     }

Open in new window


If I put a break on the return I see the good data in the table variable.  I just can't figure out the right syntax to display it in the view.
The first table is the main one and has info I may edit. That is displayed using Div's
like this
 <label asp-for="getanalmainV.FirstOrDefault().Envelope" class="control-label col-xl-1 h-100 flex-fixed-width-item text-right bg-info"></label>
            <input asp-for="getanalmainV.FirstOrDefault().Envelope" class="form-control col-xl-1 h-100 flex-fixed-width-item" />
            <span asp-validation-for="getanalmainV.FirstOrDefault().Envelope" class="text-danger"></span>

Open in new window

The second table is just a list of non editable data for reference.

thanks
Pete
* ASP .NET MVC 5ASP.NET

Avatar of undefined
Last Comment
Prakash Samariya
Avatar of Prakash Samariya
Prakash Samariya
Flag of India image

@Peter, your question is unclear,

Do you want Master-Detailed kind of multiple tables or two separate tables to be printed separately? 
Avatar of Peter Groves
Peter Groves
Flag of Canada image

ASKER

Yes , Master detailed would be good ,such as a sales order where you have the customer info that the user can change followed by the list of items on the order!  

Avatar of Peter Groves
Peter Groves
Flag of Canada image

ASKER

OK solved it.

   @foreach (var item in Model.SubTasks)
        {
            <tr>
                <td>
                   
                    @Html.DisplayFor(modelItem => item.Ident)
                </td>

Open in new window


I was over thinking it as it was quite simple!

Pete
ASKER CERTIFIED SOLUTION
Avatar of Peter Groves
Peter Groves
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Good Luck :)
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo