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
- 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?
- 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.