我对MVC和ASP.NET一般都是新手,所以我希望有人可以帮助我朝着正确的方向前进。
我正在开发一个网站,允许用户通过在下拉列表中按日期排序来查看数据库中的某些记录。我遇到了一些问题。
我不知道如何让用户首先进入视图并看不到数据,但能够从下拉列表中选择日期,然后只查看数据库中与下拉列表匹配的条目。 / p>
这是我的控制器代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using HAPtest.Models;
namespace HAPtest.Controllers
{
public class ViewerController : Controller
{
private LIVEASPNETDBEntities db = new LIVEASPNETDBEntities();
public ActionResult Index(string dateString)
{
IEnumerable<SelectListItem> byDate = db.HapRecords
.ToList()
.Select(c => new SelectListItem
{
Value = c.Payment_Date.ToString("yyyy-MM-dd"),
Text = c.Payment_Date.ToString("MM-dd-yyyy")
});
ViewBag.ByDate = byDate;
if (!string.IsNullOrWhiteSpace(dateString))
{
DateTime dt = Convert.ToDateTime(dateString);
ViewData["display"] = db.HapRecords.Where(c => c.Payment_Date == dt).ToList();
}
return View();
}
以下是我的观点代码:
@model IEnumerable<HAPtest.Models.HapRecord>
@{
ViewBag.Title = "Hap Records By Date";
}
<div id="DataDisplay">
<h2>Hap Records By Date</h2>
@using(Html.BeginForm("Index", "Viewer")) {
@Html.DropDownList("byDate", new SelectList(ViewBag.ByDate, "Value", "Text"))
<input type="submit" value="Update" />
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.RefNum)
</th>
<th>
@Html.DisplayNameFor(model => model.Payment_Status)
</th>
<th>
@Html.DisplayNameFor(model => model.Payment_Method)
</th>
<th>
@Html.DisplayNameFor(model => model.Payment_Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Payment_Number)
</th>
<th>
@Html.DisplayNameFor(model => model.Party_Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name_of_Payee)
</th>
<th>
@Html.DisplayNameFor(model => model.Trans_Code)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Trans_Amount)
</th>
<th>
@Html.DisplayNameFor(model => model.F_Initial)
</th>
<th>
@Html.DisplayNameFor(model => model.L_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Corr_Box)
</th>
<th>
@Html.DisplayNameFor(model => model.Corr_Street1)
</th>
<th>
@Html.DisplayNameFor(model => model.Corr_Street2)
</th>
<th>
@Html.DisplayNameFor(model => model.Corr_City)
</th>
<th>
@Html.DisplayNameFor(model => model.Corr_State)
</th>
<th>
@Html.DisplayNameFor(model => model.Corr_Zip)
</th>
<th>
@Html.DisplayNameFor(model => model.UploadDateTime)
</th>
<th>
@Html.DisplayNameFor(model => model.UploaderUserName)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.RefNum)
</td>
<td>
@Html.DisplayFor(modelItem => item.Payment_Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.Payment_Method)
</td>
<td>
@Html.DisplayFor(modelItem => item.Payment_Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Payment_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Party_Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name_of_Payee)
</td>
<td>
@Html.DisplayFor(modelItem => item.Trans_Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Trans_Amount)
</td>
<td>
@Html.DisplayFor(modelItem => item.F_Initial)
</td>
<td>
@Html.DisplayFor(modelItem => item.L_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Corr_Box)
</td>
<td>
@Html.DisplayFor(modelItem => item.Corr_Street1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Corr_Street2)
</td>
<td>
@Html.DisplayFor(modelItem => item.Corr_City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Corr_State)
</td>
<td>
@Html.DisplayFor(modelItem => item.Corr_Zip)
</td>
<td>
@Html.DisplayFor(modelItem => item.UploadDateTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.UploaderUserName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.HapRecordId }) |
@Html.ActionLink("Details", "Details", new { id = item.HapRecordId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.HapRecordId })
</td>
</tr>
}
</table>
</div>
当我有这样的代码时,我收到一个错误:System.NullReferenceException,这是因为我的Model在这个视图中是NULL。但是,我不知道每次用户从下拉列表中选择日期时如何使用更新的记录填充视图。
提前感谢帮助我在这里找到正确的方向。
答案 0 :(得分:2)
有几件事可以让你走上正轨。请不要把它当作一个现成的解决方案 - 这些只是指导。
您的视图是强类型的,这很好。但是为了使其正常工作,您需要为其提供模型。这是在控制器中完成的:
var model = db.HapRecords.Where(c => c.Payment_Date == dt).ToList();
return View(model);
您仍然希望在视图中同时支持这两种情况:刚打开页面时以及通过下拉菜单提交内容时。这部分在您的代码中很好:
if (!string.IsNullOrWhiteSpace(dateString))
{
DateTime dt = Convert.ToDateTime(dateString);
var model = db.HapRecords.Where(c => c.Payment_Date == dt).ToList();
return View(model);
}
else
{
return View();
}
在视图中,您需要在没有模型时处理该情况,即model为null。并且在这种情况下不要显示任何东西:
@if (Model != null)
{
<table class="table">
...
}
想想当Model中没有记录但是它不是null时你想做什么。如果您仍然不想显示任何内容,则可能需要从IEnumerable
切换到IList
以使Count
属性可用。
另请注意:
@Html.DisplayNameFor(model => model.RefNum)
您实际上是在尝试从RefNum
检索IEnumerable
。您可能想要(当然,如果Model包含任何记录):
@Html.DisplayNameFor(model => model[0].RefNum)
Controller接受名为dateString
的参数,其DropDown名为byDate
。决定一个或另一个 - 在ASP.NET MVC参数名称中很重要。
DropDown和Update按钮应该包含在提交到Viewer/Index
url的表单上,除非您没有向我们显示您向服务器提交下拉值的实际方式。