我正在自己学习ASP.NET MVC并构建一个示例Web应用程序,我从视频教程和阅读书籍中学到了这些应用程序。 我正在使用现有数据库的代码构建一个Web应用程序。当我使用viewbags时,一切都运转良好。当我试图将viewbags更改为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帖子,现在我已经达到了这一点,你看了一个字这么久,它的拼写开始看起来很奇怪。基于我读过的帖子和文章,我要么得到“值不能为空。参数名称:项目”错误或“没有类型'IEnumberable”的ViewData项错误。你们其中一位专家可以指出我需要解决的问题,让我的下拉列表工作。任何帮助都感激不尽。
答案 0 :(得分:0)
在我的get action方法中,我编辑了以下代码
var statuses = db.APPETS.Select(t => t.Status.Status1);
if(!String.IsNullOrEmpty(status))
{
aPPET1 = aPPET1.Where(t => t.Status.Status1.Contains(status));
}
到
var stats = db.Status.Select(s => s.Status1);
viewModel.Statuses = new SelectList(stats);
{
if (!String.IsNullOrEmpty(status))
{
aPPET1 = aPPET1.Where(t => t.Status.Status1.Contains(status));
}
}
我的应用程序按预期开始工作。 @Stephen感谢您的及时回复和帮助。