【发布时间】:2021-02-17 17:42:33
【问题描述】:
我正在尝试从视图访问在控制器中创建的视图模型对象列表。我知道我必须非常接近,但我不太确定如何访问它。
Index.cshtml
@model IEnumerable<CovidAppV5.ViewModel.COVIDvm>
@{
ViewBag.Title = "Index";
var VMlist = ViewData["VMlist"];
WebGrid grid = new WebGrid(VMlist, canSort: true, canPage: true, rowsPerPage: 5);
}
<meta name="viewport" content="width=device-width" />
<h2>Report</h2>
<table class="table" id="resultTable">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone1)
</th>
<th>
@Html.DisplayNameFor(model => model.OrgNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.UnableToTelework)
</th>
<th>
@Html.DisplayNameFor(model => model.CaringForMinor)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone1)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrgNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnableToTelework)
</td>
<td>
@Html.DisplayFor(modelItem => item.CaringForMinor)
</td>
</tr>
}
</table>
<div>
@grid.GetHtml()
</div>
控制器
public ActionResult Index(string Name, string Phone1)
{
//System.Diagnostics.Debug.WriteLine(Phone1);
List<COVIDvm> VMlist = new List<COVIDvm>(); // to hold list of forms
var covidQuery = (from form in db.Case_Log
where form.Name == Name
join eForm in db.Emergency_Leave on form.Name equals eForm.Name
select new {form.Name, form.Phone1, eForm.OrgNumber, eForm.UnableToTelework, eForm.CaringForMinor}).ToList();
foreach (var item in covidQuery)
{
COVIDvm objcvm = new COVIDvm(); // ViewModel
objcvm.Name = item.Name;
objcvm.Phone1 = item.Phone1;
objcvm.OrgNumber = item.OrgNumber;
objcvm.UnableToTelework = item.UnableToTelework;
objcvm.CaringForMinor = item.CaringForMinor;
VMlist.Add(objcvm);
}
ViewData["VMlist"] = VMlist;
return View();
}
我不确定你是否需要它,但如果你需要它,那就是 COVIDvm:
新冠病毒
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace CovidAppV5.ViewModel
{
public class COVIDvm
{
//All from questionaires
public string Name { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
[Column("Division/District")]
public string Division_District { get; set; }
public string OrgNumber { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public System.DateTime DateOfTest { get; set; }
public string DateOfExposure { get; set; }
//All from Emergency Leave
public bool UnableToTelework { get; set; }
public bool CaringForMinor { get; set; }
public bool SpecialCircumstance { get; set; }
public double Annual { get; set; }
public double PaidSick { get; set; }
public double EmergencyPaidSick { get; set; }
public double Unpaid { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public System.DateTime LeaveFrom { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public System.DateTime LeaveTo { get; set; }
//All from Family Leave
public bool QuarantineOrder { get; set; }
public bool AdviseToSelfQuarantine { get; set; }
public bool Symptoms { get; set; }
public bool CaringForPerson { get; set; }
public bool ChildCareUnavailable { get; set; }
public bool SimilarConditions { get; set; }
}
}
我很确定在 MVC 中,如果您没有指定您尝试使用的视图数据对象的类型(我现在没有这样做),那么它会尝试使用字符串类型,这显然会给我错误。所以我需要定义VMlist是什么类型的对象,但是我尝试了几种不同的想法都无济于事。我认为它需要类似于var VMlist = ViewData["VMlist"] as List<COVIDvm>;
或类似但不起作用的东西。
您可能还注意到,这会在 veiw 上引发一些错误:WebGrid grid = new WebGrid(VMlist, canSort: true, canPage: true, rowsPerPage: 5); ,然后再使用它:<div> @grid.GetHtml()</div>,因为我不确定 VMlist 是否是可接受的数据源,即使我在视图中正确拥有了对象.不过,我对此可能是错的,但这不是问题的主要关注点。主要关注点是将对象从控制器传递到视图。
例如,如果我想将 LINQ 查询结果 covidQuery 从控制器传递到视图怎么办?这可能吗?
【问题讨论】:
-
@jdweng 在这一行的答案中:
var grid = new WebGrid(source: (List<Tuple<int, string, int>>)ViewBag.grid1, canPage: true, canSort:false, rowsPerPage: 17);我用什么代替List<Tuple<int, string, int>>? Visual Studio 推荐这个:source: (List<CovidAppV5.ViewModel.COVIDvm>,这在技术上是可行的,但它会显示整个 View Model 而不仅仅是 VMlist。 -
做以下工作:List
你得到一个数据集,你只想要一个数据集。 -
不,列表错误:
Generic type 'List<T>' requires 1 type arguments.和 CovidAppV5 错误:CovidAppV5 is a namespace but is used like a variable。最后,由于某种原因,Viewbag 不再喜欢括号:syntax error ',' expected。
标签: c# asp.net-mvc view controller