Click here to Skip to main content
15,888,208 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Would like some assistance in regards with this, as there is not real clear help in regards with including existing model into dialog views.

I am new to C# and Razor. Any assistance would be appreciated as long as it is constructive and helpfull.

Just trying to pull my model data into the view to generate a data table on the list of items.



```
Dealer Model
```
namespace BidWheels.Data
{
    public class Dealers
    {
        public int ID { get; set; }
        public string DealershipName { get; set; }
        public string DealershipAddress { get; set; }
        public int DealershipTownID { get; set; }
        public string DealershipTown { get; set; }
        public string ContactName { get; set; }
        public string ContactSurname { get; set; }
        public string ContactNumber { get; set; }
        public string ContactNumber2 { get; set; }
        public string ContactEmailAddress { get; set; }
        public string AspNetUserId { get; set; }
        public string Email { get; set; }
    }
}


---

```
Dialog View Razor Page
```

<pre>@inject IJSRuntime JSRuntime;
@using BidWheels.Shared.Controls;
@using Microsoft.AspNetCore.Identity;
@using BidWheels.CustomProviders;
@using BidWheels.Data;
@using System.ComponentModel.DataAnnotations;
@using System.Data;
@using System.Collections.Generic;
@using System;  


...
<thead>
                   <tr>
                       <th>Dealer</th>
                       <th>Name</th>
                       <th>Surname</th>
                       <th>Email</th>
                       <th>Approval</th>
                   </tr>
               </thead>
               <tbody>
                   <tr>
                       @{
                           foreach(var item in Data.Dealers)
                           {

                           }
                        }
                   </tr>
               </tbody>

...

What I have tried:

Well Google I guess...
Trial and Error Also....
Posted
Updated 19-Feb-20 1:22am

What do you mean by "dialog views"? I don't recognise this as a known technical term. Do you just mean a regular "view"?

If so, then you specify the model you want to use in the view like this (usually as the first line in the cshtml file):

C#
@model BidWheels.Data.Dealers


That will provide you an instance of the Dealers class, but looks like you want a list. So you probably want to create a new view model class with a dealers list:

C#
public class DealersViewModel
{
    public List<Dealers> Dealers { get; set; };
}


Then in your view:

HTML
@model BidWheels.Data.DealersViewModel

...

@{
    foreach(var item in Model.Dealers)
    {
        <span>@item.DealershipName</span>
    }
}


You also need to pass the model instance to the view in your controller:

C#
public ActionResult MyDealers()
{
    var model = new DealersViewModel();
    model.Dealers = new List<Dealers>(); // Populate your list here.
    return View(model);
}
 
Share this answer
 
v3
A good way to get a head-start on this would be to simply right-click on the Collection's ActionResult method and select the Create New View option which will generally give you the HTML with all the @Razor markup needed.

You did not provide the code from the Controller so there is no way to tell exactly what you got going on- generally you would return a Model or a Collection of type Model.

Your current View is running a for...each loop across what can only be assumed to be a Dealer Collection. Then item would simply be a single instance of a Dealer and the properties should be easily accessed. This whole thing will fail though if the code you have not shared is not doing what is assumed
HTML
<tbody>
  <tr>
    @{
      foreach(var item in Data.Dealers) {
        <td>@item.DealershipName</td>
        <!-- other properties here -->
      }
    }
  </tr>
</tbody
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900