2013-12-08 104 views
0

我剛剛開始在ASP.NET MVC中使用@ Html.DropDownListFor屬性,我無法擺脫它的困擾!希望有些人可以幫我一把嗎?我試圖在ASP.NET MVC 4中重新構建這個頁面(http://goo.gl/q7H9Na)。如果任何人都可以幫助我,那麼如果您可以提供關於如何獲得「Party Size」字段的示例加工。我已經包括了我迄今爲止設法做的事情。謝謝你的幫助!在ASP.NET MVC中填充DropDownListFor的困惑

視圖模型:

namespace ThePines.ViewModels 
{ 
    public class EnquiryForm 
    { 
     [Required(ErrorMessage = "* Please enter a first name")] 
     public string FirstName { get; set; } 

     [Required(ErrorMessage = "* Please enter a last name")] 
     public string LastName { get; set; } 

     [Required(ErrorMessage = "* Please enter an email address")] 
     [EmailAddress(ErrorMessage = "* Please enter a valid email address")] 
     public string EmailAddress { get; set; } 

     [Required(ErrorMessage = "* Please enter a country")] 
     public string Country { get; set; } 

     [Required(ErrorMessage = "* Please enter a question")] 
     public string Question { get; set; } 
    } 
} 

查看:

@using (Html.BeginForm("Index", "Enquiries", FormMethod.Post, new { enctype = "multipart/form-data"})) 
{ 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <table cellpadding="8" cellspacing="8"> 
      <tr> 
       <td>@Html.LabelFor(model => model.FirstName, "First Name")</td> 
       <td>@Html.TextBoxFor(model => model.FirstName)</td> 
       <td>@Html.ValidationMessageFor(model => model.FirstName)</td> 
      </tr> 

      <tr> 
       <td>@Html.LabelFor(model => model.LastName, "Last Name")</td> 
       <td>@Html.TextBoxFor(model => model.LastName)</td> 
       <td>@Html.ValidationMessageFor(model => model.LastName)</td> 
      </tr> 

      <tr> 
       <td>@Html.LabelFor(model => model.EmailAddress, "Email Address")</td> 
       <td>@Html.TextBoxFor(model => model.EmailAddress)</td> 
       <td>@Html.ValidationMessageFor(model => model.EmailAddress)</td> 
      </tr> 

      <tr> 
       <td>@Html.LabelFor(model => model.Country, "Country")</td> 
       <td>@Html.TextBoxFor(model => model.Country)</td> 
       <td>@Html.ValidationMessageFor(model => model.Country)</td> 
      </tr> 

      <tr> 
       <td>@Html.LabelFor(model => model.PartySize, "Party Size")</td> 
       <td>@Html.DropDownListFor()</td> 
       <td>@Html.ValidationMessageFor(model => model.PartySize)</td> 
      </tr> 

      <tr> 
       <td>@Html.LabelFor(model => model.Question, "Question")</td> 
       <td>@Html.TextBoxFor(model => model.Question)</td> 
       <td>@Html.ValidationMessageFor(model => model.Question)</td> 
      </tr> 
     </table> 

     <input type="submit" value="Send Enquiry" /> 
    </fieldset> 
} 

控制器:

public class EnquiriesController : Controller 
{ 
    // 
    // GET: /Enquiries/ 

    public ActionResult Index() 
    { 

    } 

    // POST: /Enquiries/ 

    [HttpPost] 
    public ActionResult Index(EnquiryForm enquiryForm) 
    { 
     if (ModelState.IsValid) 
     { 

     } 

     return View(enquiryForm); 
    } 
} 

回答

0

首先,你需要一個PartySize屬性添加到您的視圖模型:

public int PartySize { get; set; } 

然後,在你的控制器中,你將有:

public ActionResult Index() 
{ 
    var model = new EnquiryForm(); 

    ViewBag.PartySizes = Enumearable.Range(1, 8); 
    return View(model); 
} 

而且,在你看來,你必須:

<tr> 
    <td>@Html.LabelFor(model => model.PartySize, "Party Size")</td> 
    <td>@Html.DropDownListFor(model => model.PartySize, new SelectList(ViewBag.PartySizes))</td> 
    <td>@Html.ValidationMessageFor(model => model.PartySize)</td> 
</tr> 
+0

非常感謝,這工作很棒! – smclintock

-1

創建一個類中調用

public class KeyValuePair 
{ 
    public string Text; 
    public string Text; 
} 

在獲取索引操作

List<KeyValuePair> keyValuePair = new List<KeyValuePair>(); //Add your drop down values into this list 
ViewBag.PartySize= new SelectList(keyValuePair, "Value", "Text", selectedValue: id); 

上查看頁面

<td>@Html.LabelFor(model => model.PartySize, "Party Size")</td> 
<td>@Html.DropDownList("PartySize",(SelectList)ViewBag.PartySize)</td> 
<td>@Html.ValidationMessageFor(model => model.PartySize)</td> 
+0

有沒有必要創造e KeyValuePair類。 – ataravati

0

我通過ViewBag做到這一點,但我一直在SelectList實例化代碼,它應該是,在控制器中。這是一個例子。我希望這有助於:

在查看

@Html.DropDownListFor(model => model.PartySize, ViewBag.Sizes as SelectList, "Party Size") 

在視圖模型(添加)

public int PartySize { get; set; } 

在控制器

List<int> Sizes = new List<int>(); 

     for(int i = 1; i < 9; i++) 
     { 
      Sizes.Add(i); 
     } 

     ViewBag.Sizes = new SelectList(Sizes);