1

I am learning MVC and coding my own simple application. A calculator application. Basically, I want to have the user enter two numbers then use a drop down menu to decide what operation they will conduct on the numbers. Unlike previous things I have been working on. The code below (except for the hint I found at a blog post for doing a drop down), is completely my own.

Problem

I was surprised at how nontrivial a drop down menu would end up being. So I found this nice post. I modified this code to my solution and I get the following error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

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:     }

Code

Model

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; }

  }

Controller

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

        return View(cd);
    }
}

View

@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>

Attempts

  1. I think the overload method is the big clue. Thus, I suspect my method OperationItems is messed up in my model. That being said, I'm not sure what it would be?
  2. This. Except I am thinking my model doesn't fit that problem.

Pardon any stupid mistakes. I think I am close? Webdev is making my head spin at the moment.

Community
  • 1
  • 1
hlyates
  • 1,279
  • 3
  • 22
  • 44
  • 1
    check [this](http://stackoverflow.com/a/23585628/3246007) – RadioSpace Oct 13 '14 at 02:31
  • 1
    Add a property to the model `public string Operator {get; set;}` and use `@Html.DropDownListFor(x=>x.Operator, Model.OperationItems)` –  Oct 13 '14 at 02:32
  • @StephenMuecke Exactly this, and I then reread the DropDownList() documentation. So I now have an id for my choices. There is a new problem. I am seeing four objects called 'MVCalculatorDemo.Models.CalculatorOperations', not Add, Substract, and etc. Is this because of how I wrote SelectList in my model? I rewrote it as SelectList(ops, "Name"), but same error. I'll continue to read MSDN docs. – hlyates Oct 13 '14 at 02:54
  • Never mind, I figured it out. The model needed an ID and a operation name, so that the OperationItem could be displayed. A lot of work for a droplist. :) – hlyates Oct 13 '14 at 03:16
  • What is `CalculatorOperations`? Why do you need it if it appears to have only one property `Text`. You code can be reduced to one or 2 lines. –  Oct 13 '14 at 03:17

2 Answers2

1

Well, this is pretty simple really. The error tells you the problem:

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

Here is DropDownListFor:

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

As you can see, none of those take 1 argument. The simplest version takes 3 arguments (although the first one is hidden, because it's used as part of the extension method, so you only see 2 arguments in your actual code).

A dropdownlist requires a minimum of two things, a variable to hold the selected value, and a list of items to select. In your code, you don't have a variable to hold the selected item, so how would you know what item was selected by the user?

You'll notice in the questions you linked to, the answers were to have two arguments.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Erick, thanks. I thought I checked this but confused myself with the SelectListItem. That is why I used only one argument. – hlyates Oct 13 '14 at 02:41
1

Have you actually had a look at your code? Razor MUST be highlighting the declaration of DropDownListFor as a compilation error because at the least this extension method takes in TWO arguments, the expression (property to associate from your model) and an IEnumerable<> object which will be used to populate the dropdown items. This should do...

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

For more info, check the documentation for DropDownListFor

Leo
  • 14,625
  • 2
  • 37
  • 55
  • 1
    Yes, I have been looking at my code. I'm new to this and after hours of reading and coding, my brain is about to explode. This helped me a lot and even though I immediately ran into another problem. This helped me solve the entire thing. Now on to the next part which is to make a custom view with the results of my calculations. :) – hlyates Oct 13 '14 at 03:17