2

我需要将以下内容转换为下拉列表的@html 助手。

<select size="30" class="scrollableinside" id="CustomerSelect">
    @foreach (var customer in Model.Customers)
    { 
        <option  value=@customer.CustomerContacts.First().CustomerContactID>@customer.CustomerName &#09;&emsp;&#09; @customer.CustomerContacts.First().Phone</option>
    }
</select>

由于我使用 .First() 和将 2 条数据放入列表中这样做有点不正统,所以我不确定如何为此制作 @Html.SelectListFor 。

@Html.DropDownListFor(model => model.CustomerID, new SelectList(Model.Customers, "CustomerID", "What do I put here"))
4

2 回答 2

4

你应该试试这个

public class IndexViewModel
{
    // Stores the selected value from the drop down box.
    [Required]
    public int CountryID { get; set; }

    // Contains the list of countries.
    public SelectList Countries { get; set; }
}

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        IndexViewModel viewModel = new IndexViewModel();
        viewModel.Countries = new SelectList(GetCountries(), "ID", "Name");
        return View(viewModel);
    }

    [HttpPost]
    public ActionResult Index(IndexViewModel viewModel)
    {
        viewModel.Countries = new SelectList(GetCountries(), "ID", "Name");
        if (!ModelState.IsValid)
            return View(viewModel);

        //TODO: Do something with the selected country...
        CMSService.UpdateCurrentLocation(viewModel.CountryID);

        return View(viewModel);
    }
}

在视图中

@Html.DropDownListFor(x => x.CountryID, Model.Countries)

或者你可以使用另一个

@Html.DropDownListFor(x => x.CountryID, Model.Countries, "- please select -")
于 2013-04-05T05:41:09.163 回答
-1
@Html.DropDownListFor(model => model.CustomerID,new SelectList(Model.Customers, "datavaluefield","datatextfield", Model.CustomerID))
于 2013-04-05T06:06:54.207 回答