DropDownListFor generates 'The name 'model' does not exist in the current context' error - asp.net-mvc-3

Just starting out using MVC3 and hit a problem trying to build a drop-down in a view.
The ViewModel is populated with a SelectList of items:
mdm.CaptionSetList=new SelectList(CaptionSet.Fetch(), "CaptionSetId", "Description")
which then is used in the view:
#Html.DropDownListFor(model => model.Entity.CaptionSetId, model.CaptionSetList)
but when the page is hit, the line is highlighted with the compiler message:
Compiler Error Message: CS0103: The name 'model' does not exist in the current context
What's the beginner's mistake I'm making?

The first argument for DropDownListFor is a function so that part is right, but the second part is just expecting a SelectList so all you need to do is
#Html.DropDownListFor(model => model.Entity.CaptionSetId, Model.CaptionSetList)
Note the capitalization.
Further clarification
In a strongly typed view Model is a property that refers to the model bound to the view. Since the second argument is just expecting a list and you've specified that the Model has a property called CaptionSetList, You specify the list as Model.CaptionSetList. If you had put the list in the ViewBag, you would put ViewBag.CaptionSetList.
Contrast this to the first argument which is a function that takes one argument of the same type as the model.

Got the same error.
In my case, I had done some search and replace and forgot to change the lambda expression:
#Html.DropDownListFor(m => model.StagingDelegate.ManagerId, new[]{
new SelectListItem() {Text = "ManagerId 1", Value = "1"},
new SelectListItem() {Text= "ManagerId 2", Value= "2"},
}, "Choose a Manager", new { #class = "form-control form-control-sm " })
Replace m => with model =>:
#Html.DropDownListFor(model => model.StagingDelegate.ManagerId, new[]{
new SelectListItem() {Text = "ManagerId 1", Value = "1"},
new SelectListItem() {Text= "ManagerId 2", Value= "2"},
}, "Choose a Manager", new { #class = "form-control form-control-sm " })

Related

Use a watermark in a TextBox

This is my code:
#Html.TextBox("Email", new { placeholder = "Email", Title = "Email" })
It does not work - when I run it, the browser displays the text inside the TextBox, and the HTML content that is: ("placeholder = "Phone", Title = "Phone" ").
You are using the wrong overload of the TextBox helper. The second argument is the value and the third argument are the html properties.
So, here's how to fix it:
#Html.TextBox("Email", null, new { placeholder = "Email", title = "Email" })
I think your confusion stems from the fact that the strongly typed TextBoxFor helper takes 2 arguments:
#Html.TextBoxFor(x => x.Email, new { placeholder = "Email", title = "Email" })

MVC3: Set Dropdown list Selected Value

I am using mvc3 and I have a drop down list in my view.
#Html.DropDownListFor(m => m.State,
new SelectList(Model.StateList, "Value", "Text"))
Is there a way of setting the selected value in the View?
Extending on what Romias said, in your controller, set Model.State to whatever value you want. If you wanted 'WI', then Model.State should equal that.
Controller:
public ActionResult Index()
{
var m = new TestViewModel();
m.State = "WI";
return View(m);
}
View:
#Html.DropDownListFor(m => m.State, new SelectList(Model.StateList, "Value", "Text", Model.State))
Just do:
#Html.DropDownListFor(m => m.State, new SelectList(Model.StateList, "Value", "Text", Model.State))

MVC Razor - use #Html.DropDownListFor() for the 12 months of the year

I am quite new to the HTML helper extensions in MVC, let alone in Razor, and I'm looking for a simple, neat way to display a dropdown list for the 12 months of the year, with the value as the number and the text the "MMM" representation of the date.
So the HTML at the end should be like:
<select>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<!-- etc -->
</select>
Is it possible to do this entirely in the view? I need an answer that users the #Html.DropDownListFor() so I can take advantage of automatic model binding. I get close with the line of code below, but it doesn't have the distinct values.
#Html.DropDownListFor(model => Model.DobMonth, new SelectList(Enumerable.Range(1,12).Select(r => new DateTime(2000, r, 1).ToString("MMM"))), "- -")
For non-technical reasons, I can't use a jQuery datepicker.
You are almost there, the problem is that if you don't provide the dataValue and dataText parameters when creating the SelectList it will just call ToString on the items and use them as the option's text.
What you need is to return Text, Value pairs from your select:
#Html.DropDownListFor(model => Model.DobMonth, new SelectList(
Enumerable.Range(1, 12)
.Select(r => new
{
Text = new DateTime(2000, r, 1).ToString("MMM"),
Value = r.ToString()
}),
"Value", "Text", Model.DobMonth))
If you want to have an "empty" item you need to add it manually:
#Html.DropDownListFor(model => Model.DobMonth, new SelectList(
new [] { new { Text = "- -", Value = (string)null } }.Concat(
Enumerable.Range(1, 12)
.Select(r => new
{
Text = new DateTime(2000, r, 1).ToString("MMM"),
Value = r.ToString()
})),
"Value", "Text", Model.DobMonth))

MVC3 #Html.DropDownListFor not populating selected item

I have the following dropdown list in an MVC3 application and the selected item is not showing a value. My dropdown list is contained in a list of a custom class. My view code is as follows.
...
for (int i = 0; i < Model.PartAttributes.Count; i++)
{
if (Model.PartAttributes[i].IsActive)
{
<div class="row inline-inputs">
<label class="span5">#Model.PartAttributes[i].DisplayName:</label>
#Html.DropDownListFor(m => m.PartAttributes[i].Value, Model.PartAttributes[i].PartList, "Choose Part")
#Html.TextBoxFor(model => Model.PartAttributes[i].Value, new { #class = "mini" })
#Html.ValidationMessageFor(model => Model.PartAttributes[i].Value)
#Html.HiddenFor(model => model.PartAttributes[i].AttributeName)
</div>
}
}
...
The text box under the dropdown box fills correctly and the list options fill correctly. And the selected option is in the list of options to pick. What can I be doing wrong?
Try like that:
#Html.DropDownListFor(
m => m.PartAttributes[i].Value,
new SelectList(
Model.PartAttributes[i].PartList,
"Value",
"Text",
Model.PartAttributes[i].Value
),
"Choose Part"
)
AFAIK the DropDownListFor helper is unable to determine the selected value from the lambda expression that is passed as first argument if this lambda expression represents complex nested properties with collections. Works with simple properties though: m => m.FooBar. I know that it kinda sucks, but hopefully this will be fixed in future versions.

MVC DropDownListFor() Selected Item is not Selected / Required Validation not run

I am having trouble getting my DropDownList to set the selected item to the value from the model.
The field in the model is just a string for the Title of the users name (Mr, Miss etc..) Below is my code so far.
<td>
#{ var list = new List<SelectListItem>(new[] {
new SelectListItem{ Selected = string.IsNullOrEmpty(Model.Title), Text="",Value=""},
new SelectListItem{ Selected = Model.Title.Equals("Mr"), Text="Mr",Value="Mr"},
new SelectListItem{ Selected = Model.Title.Equals("Mrs"), Text="Mrs",Value="Mrs"},
new SelectListItem{ Selected = Model.Title.Equals("Miss"), Text="Miss",Value="Miss"},
new SelectListItem{Selected = Model.Title.Equals("Ms"), Text="Ms",Value="Ms"}
});
}
#Html.DropDownListFor(m=>m.Title, list)
</td>
I had this problem with MVC 3 and it turned out that I had set ViewBag.Title on my View (using it for the page title). As soon as I changed it to ViewBag.PageTitle, the dropdownlist code started working : #Html.DropDownListFor(model => model.Title, Model.MySelectList)
The reason for this is that in MVC 2/3, any ViewBag / ViewData properties with the same name as those in the Model object get used in preference in DropDownListFor(), so you need to rename them to make sure they don't conflict. Because that seems really flaky, I just stopped using ViewBag entirely and now rely only on the View Model for passing stuff into the View.
The reason this problem is so prevalent is that ViewBag.Title is used in many introductory tutorials and demo code to set the HTML title element, and so inevitably gets adopted as a "best-practice" approach. However, Title is a natural Model property name for use in dropdowns on a "User Details" view.
So it turns out that the only reason it doesn't work is because my field name is Title, I changed it to Prefix and my exact code works. Way too much time spent finding that out...
Here is working code.
<td>
#{ var list = new List<SelectListItem>(new[] {
new SelectListItem {
Selected = string.IsNullOrEmpty(Model.Prefix),
Text="",
Value=""
},
new SelectListItem {
Selected = Model.Prefix.Equals("Mr"),
Text="Mr",
Value="Mr"
},
new SelectListItem {
Selected = Model.Prefix.Equals("Mrs"),
Text="Mrs",
Value="Mrs"
},
new SelectListItem {
Selected = Model.Prefix.Equals("Miss"),
Text="Miss",
Value="Miss"
},
new SelectListItem {
Selected = Model.Prefix.Equals("Ms"),
Text="Ms",
Value="Ms"
}
});
}
#Html.DropDownListFor(m => m.Prefix, list)
</td>

Resources