why this not worked in asp.net MVC 3?
i have a project who i migrate to MVC 3 from 2.
if i write same in fresh MVc 3 project that they are work fine with but my migrated project not worked for this code
<select id="selrecipetype">
@foreach (custom.Type type in rectypes)
{
<option value="@(type.ID)">@(type.Type)
</option>
}
</select>
Compiler Error Message: CS1525: Invalid expression term ';'
Line 217: <option value="@(type.ID)"&g开发者_开发百科t;@(type.Type)
Line 218: </option>
Line 219: }
Line 220: </select>
so what is wrong gone happened in this code.
if i write this this same not worked
@foreach (Categories.Article category in categories) {@(category.Name) }
i want to know why it's not worked and i got error when work fine if i do in fresh MVC 3 project
Erm, such code doesn't have to exist, so why care whether it works or not? Not to mention that this <select>
doesn't have a name meaning that other than using javascript the selected value will never be posted to the server. So start by simply selecting this code in the IDE and pressing the Del.
button on your keyboard.
The proper way to generate drop down lists in ASP.NET MVC is using the Html.DropDownListFor
helper:
@Html.DropDownListFor(
x => x.SomePropertyOnYourViewModelToBindTheResultTo,
new SelectList(rectypes, "ID", "Type")
)
or if you don't have a strongly typed view (which of course you should, but who knows):
@Html.DropDownList(
"SomeProperty",
new SelectList(rectypes, "ID", "Type"),
)
You should really do your select list using the HTML class. You should first create your select list in the controller like the following:
var types = typeRepo.GetRecTypes();
ViewBag.SelectList = new SelectList(types, "KeyFieldName", "TitleFieldName");
then in your view:
@Html.DropDownList("SelectListName", (SelectList)ViewBag.SelectList)
精彩评论