简体   繁体   中英

Showing multiple tables with partial views. Asp.Net Mvc

I want to use 3 partial views to display my search result(Client name) in 3 different tables. I do not want to show any info from the tables before the search has been completed. As i have understood you can only use one model per view, unless you make some workarounds.

I have tried to make a view model so i can reference the multiple tables from both the index and the partial views. But can't figure it out. Since the methods for the 3 different tables will be the pretty much same i am only going to post the code for the client table. Any help or pointers would be much appreciated

ViewModel:

public class ViewModel
    {

        public List<Clients> allClients { get; set; }
        public List<OrderLines> allOrders { get; set; }
        public List<ViewNewOrderSum> allViewNewOrderSum { get; set; }

    }

HomeController:

using testForAutofill.Models;

public class HomeController : Controller
    {
        test_Db_Context db = new test_Db_Context();
        // GET: Home
        public ActionResult Index()
        {
            ViewModel vm = new ViewModel();
            vm.allClients = GetClients();
            return View();
        }

     private List<Clients> GetClients()
        {
            List<Clients> clientList = new List<Clients>();
            clientList = db.Clients.ToList();
            return clientList;
        }

[HttpPost]
        public ActionResult Index(string searchTerm)
        {

            Scaleit_Db_Context db = new Scaleit_Db_Context();
            List<Clients> orderSums;
            if (string.IsNullOrEmpty(searchTerm))//Fix this!
            {
                orderSums = db.Clients.ToList();
            }
            else
            {
                orderSums = db.Clients.Where(x => x.Name.Equals(searchTerm)).ToList();
            }
            return View(orderSums);
        }

IndexView:

@using testForAutofill.Models;
@model testForAutofill.Models.ViewModel

@if (Model.allClients != null && Model.allClients.Count() > 0)
{
@using (Html.BeginForm())
{
    <b>Kundenavn:</b>
    @Html.TextBox("searchTerm", null, new { id = "txtSearch" })
    <input type="submit" value="🔍 Search" class="btn btn-primary" id="btn-search" />
@using (Html.BeginForm())
{

    <div class="card-container">
        <div class="card border-primary mb-3 card-client" style="max-width: 40rem;">
            <div class="card-header">Kunde</div>
            <div class="card-body">
                <table class="table table-hover">
                    @foreach (Clients clients in Model.allClients)
                     {
                      @Html.Partial("_Client", clients)
                     }
                </table>

            </div>
        </div>
    </div>
}

_Client:

@model testForAutofill.Models.ViewModel

//Tried both DisplayFor and not.
    <tr>
        <th>Kunde:</th>
        <td>@Html.Model.allClients.Name</td>// This is where the error gets 
                                            // thrown.
    </tr>
    <tr>
        <th>Org.nr:</th>
        <td>@Html.DisplayFor(modelItem => clients.OrgNr)</td> 
    </tr>
    <tr>
    <th>Adresse:</th>
        <td>@Html.DisplayFor(modelItem => clients.Address1)</td>
        @if (clients.Address2 != null)
        {
        <td>@Html.DisplayFor(modelItem => clients.PostNr)</td>
        }
    </tr>

The program/webpage doesnt run, and i get the error message:

" CS1061: 'HtmlHelper' does not contain a definition for 'Model' and no extension method 'Model' accepting a first argument of type 'HtmlHelper' could be found (are you missing a using directive or an assembly reference?)".

At the commented line in the Partial view.

TRY: Set object on viewData

inside controller:

    ActionResult SomeView(){

     ViewData["object"] = theObj;
     return View();

    }

inside cshtml:

@using objectNamespace

@(((objectType)ViewData["object"]).name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM