簡體   English   中英

ViewModel 顯示下拉列表不起作用

[英]ViewModel to display dropdownlist not working

我正在自己學習 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 的帖子,現在我已經到了那個時候,你看一個詞這么久,它的拼寫開始看起來很奇怪。 根據我閱讀的帖子和文章,我要么得到

值不能為空。 參數名稱:項目

錯誤或

沒有“IEnumerable”類型的 ViewData 項

錯誤。 你們中的任何一位專家都可以指出我需要修復什么才能使我的下拉列表正常工作。 任何幫助將不勝感激。

在我的 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 感謝您的及時回復和幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM