下面是我的 View 中的代码。无论出于何种原因,模型属性(即 .TableName
)都不会被识别。以下是错误消息:
CS1061: 'PagedList.IPagedList<Monet.Models.FollowUpItems>' does not contain a definition for 'TableName' and no extension method 'TableName' accepting a first argument of type 'PagedList.IPagedList<Monet.Models.FollowUpItems>' could be found (are you missing a using directive or an assembly reference?)
这是 View 索引页面中的代码。需要此页面来返回 SQL 表 FollowUpItems
中的项目列表。 :
@model PagedList.IPagedList<Monet.Models.FollowUpItems>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using PagedList.Mvc;
@using PagedList;
<h2>Follow Up Items</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<span id="searchBox" class="boxMe" >
<form method="post">
@Html.DropDownListFor(model => model.TableName, (SelectList)ViewBag.tableID)
@Html.DropDownListFor(m => m.IssueType, (SelectList)ViewBag.issueID)
@Html.DropDownListFor(m => m.Status, (SelectList)ViewBag.statusID)
<input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" />
<a href="#" style="padding-left: 30px;"></a>
</form>
</span>
<br />
<br />
<span id="programExplanation" style="width: 500px; float:left; padding: 5px; margin-left: 25px;"></span>
<span class="error" style="clear: both;">
@ViewBag.ErrorMessage
</span>
<span class="msg">
@ViewBag.Message
</span>
<br />
<br />
<br />
}
相关下拉列表用作原始项目列表的排序标准。可选择的下拉项不会反射(reflect)在数据库中,但每个下拉项代表的特定属性都存在。为了更好地衡量,这里是 FollowUpItems
型号
public partial class FollowUpItems
{
public int Id { get; set; }
public string TableName { get; set; }
public string IssueType { get; set; }
public string Status { get; set; }
public string Message { get; set; }
public string CreatedBy { get; set; }
public System.DateTime CreatedOn { get; set; }
public string LastUpdateBy { get; set; }
public Nullable<System.DateTime> LastUpdateOn { get; set; }
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Notes { get; set; }
}
这是来自 Controller 的代码:
//Sort info
string table = String.IsNullOrWhiteSpace(dropDownSelection["Table"]) ? "%" : dropDownSelection["Table"];
string issue = String.IsNullOrWhiteSpace(dropDownSelection["IssueType"]) ? "%" : dropDownSelection["IssueType"];
string status = String.IsNullOrWhiteSpace(dropDownSelection["Status"]) ? "%" : dropDownSelection["Status"];
//Set dropdown list items based on previous values
var tableOptions = new[] { new { Text = "--Table Name--", Value = "%" }, new { Text = "CE", Value = "AgentContEd" }, new { Text = "AgentProductTraining", Value = "C" } };
var issueOptions = new[] { new { Text = "--Issue Type--", Value = "%" }, new { Text = "Warning", Value = "W" }, new { Text = "Error", Value = "E" } };
var statusOptions = new[] { new { Text = "--Status Type--", Value = "%" }, new { Text = "Open", Value = "O" }, new { Text = "Under Review", Value = "U" } };
ViewBag.tableID = new SelectList(tableOptions, "Value", "Text", table);
ViewBag.issueID = new SelectList(issueOptions, "Value", "Text", issue);
ViewBag.statusID = new SelectList(statusOptions, "Value", "Text", status);
最佳答案
您的问题是您正在尝试访问实际模型中不存在的属性。 TableName、IssueType 和 Status 是列表中项目的一部分,而不是列表本身。当您发布表单时,您将如何检索您的过滤器信息?下拉列表实际上并不是您的项目的一部分,而是您要应用的过滤器的一部分。
您应该让 viewmodel 来处理您的所有 UI 信息 - 这实际上是您想要访问的信息。
public class YourViewModel
{
public PagedList.IPagedList<Monet.Models.FollowUpItems> itemList {get;set;}
public string TableName {get;set;}
public string IssueType {get;set;}
public string Status {get;set;}
}
然后,您将 View 模型发布到 Controller 操作,并根据选择处理过滤器。
当您在那里时,您还可以将选择列表保存在 View 模型中,而不是 View 包中,从长远来看,它可能会让生活变得更轻松。
关于asp.net-mvc-3 - 模型变量无法识别 ASP MVC3 DropDownListFor 中的模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15142033/