我剛剛開始在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);
}
}
非常感謝,這工作很棒! – smclintock