ViewModel 显示下拉列表不起作用

ViewModel to display dropdownlist not working

我正在自学 ASP.NET MVC 并构建我从视频教程和阅读书籍中学到的示例 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; }

我知道我遗漏了什么,但不知道是什么。我读了很多 Whosebug 帖子,现在我已经到了这样的地步,你看一个词这么久,它的拼写开始看起来很奇怪。根据我阅读的帖子和文章,我得到

Value cannot be null. Parameter Name:items

错误或

There is no ViewData item of type 'IEnumerable

错误。你们中的任何专家都可以指出我需要修复什么才能让我的下拉列表正常工作吗?任何帮助将不胜感激。

在我的获取操作方法中,我编辑了以下代码

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 感谢您的及时回复和帮助。