<div class="editor-label">
@Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Category)
@Html.ValidationMessageFor(model => model.Category)
</div>
This gives me a label and a text box. How can I get a drop down list with static select items in place of the text box. Please help. I am new to ASP.NET MVC. I need solution / advice in Razor syntax.
For that add a View by right-clicking inside ActionResult and select AddView and provide its name as Index. After adding the View add a Namespace to the Model as shown below. Here we can directly access the MobileList from the Model. Now just run the application and just check it.
@Html.DropDownListFor( model => model.Category, new SelectList(new [] {"cat1", "cat2", "cat3"}) );
Here is how you would populate a DropDownList using Model, View and Controller.
First the view
@using Website.Models
@model PageWithSelectList
@{
ViewBag.Title = "Index";
}
@Html.DropDownList("DayOfWeek", Model.DaysOfWeek)
Then the Controller and Action method
using System.Web.Mvc;
using Website.Models;
namespace Website.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new PageWithSelectList();
model.DayOfWeek = 3;
return View(model);
}
}
}
and the HTML output
<select id="DayOfWeek" name="DayOfWeek">
<option value="1">Sunday</option>
<option value="2">Monday</option>
<option selected="selected" value="3">Tuesday</option>
<option value="4">Wednesday</option>
<option value="5">Thursday</option>
<option value="6">Friday</option>
<option value="7">Saturday</option>
</select>
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With