C# 在ASP.NET MVC中填充DropDownListFor的混淆
我刚开始使用ASP.NET MVC,在@Html.DropDownListFor属性方面遇到了一些困难,我无法理解它!希望有人能帮我一把 我正在尝试在ASP.NETMVC4中重新构建此页面()。如果有人能帮助我,如果你能提供一个如何让“团队规模”实地工作的例子,那就太好了。我已经包括了到目前为止我所做的事情。谢谢你的帮助 查看模型:C# 在ASP.NET MVC中填充DropDownListFor的混淆,c#,asp.net-mvc,asp.net-mvc-4,html.dropdownlistfor,C#,Asp.net Mvc,Asp.net Mvc 4,Html.dropdownlistfor,我刚开始使用ASP.NET MVC,在@Html.DropDownListFor属性方面遇到了一些困难,我无法理解它!希望有人能帮我一把 我正在尝试在ASP.NETMVC4中重新构建此页面()。如果有人能帮助我,如果你能提供一个如何让“团队规模”实地工作的例子,那就太好了。我已经包括了到目前为止我所做的事情。谢谢你的帮助 查看模型: namespace ThePines.ViewModels { public class EnquiryForm { [Requir
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);
}
}
查看:
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);
}
}
创建一个名为
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);
List keyValuePair=new List()//将下拉列表中的值添加到此列表中
ViewBag.PartySize=新的选择列表(keyValuePair,“值”,“文本”,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>
@Html.LabelFor(model=>model.PartySize,“Party Size”)
@Html.DropDownList(“PartySize”,(SelectList)ViewBag.PartySize)
@Html.ValidationMessageFor(model=>model.PartySize)
首先,您需要向ViewModel添加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>
在您看来,您将拥有:
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>
@Html.LabelFor(model=>model.PartySize,“Party Size”)
@Html.DropDownListFor(model=>model.PartySize,新选择列表(ViewBag.PartySizes))
@Html.ValidationMessageFor(model=>model.PartySize)
我通过一个ViewBag
来实现这一点,但我将SelectList
实例化代码保留在控制器中的适当位置。这里有一个例子;我希望这有助于:
在视图中
@Html.DropDownListFor(model => model.PartySize, ViewBag.Sizes as SelectList, "Party Size")
视图模型(添加)
在控制器中
List<int> Sizes = new List<int>();
for(int i = 1; i < 9; i++)
{
Sizes.Add(i);
}
ViewBag.Sizes = new SelectList(Sizes);
列表大小=新列表();
对于(int i=1;i<9;i++)
{
增加(i);
}
ViewBag.size=新选择列表(尺寸);
无需创建KeyValuePair
类。