[英]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. 我想使用3个局部视图在3个不同的表中显示我的搜索结果(客户端名称)。 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. 由于3个不同表的方法几乎相同,因此我只发布客户机表的代码。 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: 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: 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?)". “ CS1061:'HtmlHelper'不包含'Model'的定义,找不到可以接受类型为'HtmlHelper'的第一个参数的扩展方法'Model'(您是否缺少using指令或程序集引用?)”。
At the commented line in the Partial view. 在部分视图中的注释行。
TRY: Set object on viewData 尝试:在viewData上设置对象
inside controller: 内部控制器:
ActionResult SomeView(){
ViewData["object"] = theObj;
return View();
}
inside cshtml: 在cshtml中:
@using objectNamespace
@(((objectType)ViewData["object"]).name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.