【发布时间】:2018-04-09 21:45:36
【问题描述】:
我正在自学 ASP.NET MVC,并构建了一个从视频教程和阅读书籍中学到的示例 Web 应用程序。
我首先使用现有数据库的代码构建 Web 应用程序。当我使用 viewbags 时,一切都运行良好。当我尝试将 viewbag 更改为 viewmodel 时,我的代码开始中断。
我的模型看起来像:
public partial class APPET1
{
[Column("Doc Number")]
[StringLength(255)]
public string Doc_Number { get; set; }
[StringLength(255)]
public string CCode { get; set; }
public DateTime? Date { get; set; }
[StringLength(255)]
public string Remark { get; set; }
public int? StatusID { get; set; }
public virtual Status Status { get; set; }
}
这是我的看法:
<table>
<tr>
@using (Html.BeginForm("Index", "APPET1", FormMethod.Get))
{
<td>
<tr>
<td width="200px"> <label>Search Everything </label></td>
<td> @Html.TextBox("Search", null)</td>
<td width="200px"><label>Search Document Number Only </label></td>
<td>@Html.TextBox("DocNumber", null)</td>
</tr>
<tr>
<td width="200px"><label>Remark Contains </label></td>
<td>@Html.TextBox("Remark", null)</td>
<td>Status</td>
<td>@Html.DropDownListFor(t=>t.Status, Model.Statuses, "Select Status")</td>
</tr>
</td>
<tr>
<td><button type="submit" class="btn btn-primary" style="font-weight:bold">Search</button></td>
..........some more codes .............
}
</tr>
</table>
我的控制器中的 IndexAction 方法如下所示:
public class APPET1Controller : Controller
{
private APContext db = new APContext();
// GET: APPET1
public ActionResult Index(string search, string cagecode, string sortBy, int? page, string docNumber, string remark, string status)
{
APPETViewModel viewModel = new APPETViewModel();
var aPPET1 = db.APPET1.Include(t =>T.APPETMedia)
.Include(t => t.Status)
.Include(t => t.APPETCCode)
.Include(t => t.APPETDType);
DateTime searchDate;
if (!String.IsNullOrEmpty(search))
{
bool isDateSearch = DateTime.TryParse(search, out searchDate);
if (isDateSearch)
{
aPPET1 = aPPET1.Where(s => s.Date_Received == searchDate);
}
else
{
aPPET1 = aPPET1.Where(t.Doc_Number.Contains(search)
|| t.Status.Status1.Contains(search)
|| t.Remark.Contains(search)
|| t.CCode.Contains(search));
//ViewBag.Search = search;
viewModel.Search = search;
}
}
var statuses = db.APPETS.Select(t => t.Status.Status1);
if(!String.IsNullOrEmpty(status))
{
aPPET1 = aPPET1.Where(t => t.Status.Status1.Contains(status));
}
if (!String.IsNullOrEmpty(docNumber))
{
aPPET1 = aPPET1.Where(t => t.Doc_Number.Contains(docNumber));
}
if (!String.IsNullOrEmpty(remark))
{
aPPET1 = aPPET1.Where(t => t.Remark.Contains(remark));
}
............some more codes.........
return View(viewModel);
}
在我的 ViewModel 中我添加了:
public string Status { get; set;}
public IEnumerable<SelectListItem> Statuses { get; set; }
我知道我错过了什么,但不知道是什么。我阅读了许多 stackoverflow 帖子,现在我已经到了这样一个地步,你看一个单词这么久,它的拼写开始看起来很奇怪。根据我阅读的帖子和文章,我要么得到
值不能为空。参数名称:items
错误或
没有“IEnumerable”类型的 ViewData 项
错误。你们中的任何一位专家都可以指出我需要修复什么才能使我的下拉列表正常工作。任何帮助将不胜感激。
【问题讨论】:
-
Model.Statuses的值在哪里设置? (它的null,因此错误) -
您需要在 GET 操作中将
viewModel.Statuses属性设置为SelectListItem列表(从状态集合中生成)。 -
请参阅this answer 中的控制器代码以了解您应该如何设置它
-
@Stephen,感谢您的帮助。我也在阅读你 2016 年的帖子,这也有帮助。我为 viewModel.Statuses 设置了值,它开始正常工作。
-
问题中没有答案(我已回滚您的更改)
标签: c# asp.net-mvc entity-framework-5