我刚刚开始使用ASP.NET MVC而且我一直很难使用@ Html.DropDownListFor属性,我无法理解它!希望有些人可以帮我一把?
我正在尝试在ASP.NET MVC 4中重新构建此页面(http://goo.gl/q7H9Na)。如果有人可以帮助我,那么如果你能提供一个如何获得“Party”的例子会很棒大小“现场工作。我已经包括了我到目前为止所做的事情。谢谢你的帮助!
查看型号:
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 :(得分:0)
首先,您需要将PartySize属性添加到ViewModel:
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>
答案 1 :(得分:0)
我通过ViewBag
执行此操作,但我将SelectList
实例化代码保留在控制器中。这是一个例子;我希望这会有所帮助:
在视图中
@Html.DropDownListFor(model => model.PartySize, ViewBag.Sizes as SelectList, "Party Size")
在ViewModel中(添加)
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);
答案 2 :(得分:-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>