簡體   English   中英

為什么我的方法“ DropDownListFor”沒有重載?

[英]Why am I getting a no overload for method "DropDownListFor'?

我正在學習MVC並編寫自己的簡單應用程序。 計算器應用程序。 基本上,我希望用戶輸入兩個數字,然后使用一個下拉菜單來決定他們將對這些數字執行的操作。 與以前的工作不同,我一直在努力。 下面的代碼(除了我在博客文章中找到的用於下拉的提示)完全是我自己的。

問題

我對下拉菜單最終變得多么簡單感到驚訝。 所以我找到了這個不錯的帖子 我將此代碼修改為解決方案,並收到以下錯誤:

說明:編譯服務於此請求所需的資源期間發生錯誤。 請查看以下特定的錯誤詳細信息,並適當地修改您的源代碼。

Compiler Error Message: CS1501: No overload for method 'DropDownListFor' takes 1 arguments

Source Error:


Line 18:         <p>First Number : @Html.TextBoxFor(x => x.Number1) </p>
Line 19:         <p>Second Number: @Html.TextBoxFor(x => x.Number2)</p>
Line 20:         <p>Select Operand: @Html.DropDownListFor(x=>x.OperationItems)</p>
Line 21:         <input type="submit" value="Enter" />
Line 22:     }

模型

public class CalculatorData
{
    private readonly List<CalculatorOperations> ops = new List<CalculatorOperations>();

    //Does this have to be public or not? 
    public string Number1 { get; set; }
    public string Number2 { get; set; }

    public IEnumerable<SelectListItem> OperationItems
    {
        get { return new SelectList(ops); }
    }
    //Database context connector? 

    public CalculatorData()
    {
        ops.Add(new CalculatorOperations { Name = "Add" });
        ops.Add(new CalculatorOperations { Name = "Subtract" });
        ops.Add(new CalculatorOperations { Name = "Multiply" });
        ops.Add(new CalculatorOperations { Name = "Divide" });
    }

}

  public class CalculatorOperations
  {
        public string Name { get; set; }

  }

調節器

public class CalculatorController : Controller
{
    //
    // GET: /Calculator/
    public ActionResult Index()
    {
        CalculatorData cd = new CalculatorData();

        return View(cd);
    }
}

視圖

@model MVCalculatorDemo.Models.CalculatorData

@{
    Layout = null; 
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RsvpForm</title>
</head>
<h2>Calculator</h2>
<body>
    @using (Html.BeginForm())
    {
        <p>First Number : @Html.TextBoxFor(x => x.Number1) </p>
        <p>Second Number: @Html.TextBoxFor(x => x.Number2)</p>
        <p>Select Operand: @Html.DropDownListFor(x=>x.OperationItems)</p>
        <input type="submit" value="Enter" />
    }
</body>
</html>

嘗試

  1. 我認為重載方法是個大提示。 因此,我懷疑我的方法OperationItems在模型中被弄亂了。 話雖這么說,我不確定會是什么?
  2. 這個 除了我認為我的模型不適合那個問題。

請原諒任何愚蠢的錯誤。 我想我很近嗎? Webdev現在讓我轉頭。

好吧,這真的很簡單。 該錯誤告訴您問題所在:

No overload for method 'DropDownListFor' takes 1 arguments
                                         ^^^^^^^^^^^^^^^^

這是DropDownListFor:

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor(v=vs.118).aspx

如您所見,這些都不接受1個參數。 最簡單的版本需要3個參數(盡管第一個是隱藏的,因為它被用作擴展方法的一部分,因此您在實際代碼中僅看到2個參數)。

下拉列表至少需要兩件事,一個用於保存所選值的變量,以及一個要選擇的項目列表。 在您的代碼中,您沒有變量來保存所選項目,那么您如何知道用戶選擇了哪個項目?

您會在鏈接到的問題中注意到,答案有兩個參數。

您實際上看過代碼了嗎? Razor必須突出顯示DropDownListFor的聲明是編譯錯誤,因為至少此擴展方法需要接受兩個參數,表達式(與模型關聯的屬性)和IEnumerable<>對象,該對象將用於填充下拉項。 這應該做...

@Html.DropDownListFor(x=>x.OperationItems, Model.OperationItems)

有關更多信息,請查看DropDownListFor的文檔。

暫無
暫無

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

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