这在WebForms中很容易(ish)并且在WPF中没有问题,但我在尝试在MVC中填充DropDown时遇到问题,然后从数据库中选择值..
数据库字段返回到控制器,传递给模态并输入到视图中,如此
@code
Dim vProspects As CustomerService.QuestionnaireModals = CType(ViewData("QuestionnaireModals"), CustomerService.QuestionnaireModals)
Dim vName As String = vProspects.Prospect_Name
Dim vAddress As String = vProspects.Prospect_Address
Dim vTown As String = vProspects.Prospect_Town
Dim vState As String = vProspects.Prospect_State
Dim vZip As String = vProspects.Prospect_Zip
Dim vTelephone As String = vProspects.Prospect_Telephone
Dim vCompany As String = vName & " <br/> "
vCompany += vAddress & " <br/> "
vCompany += vTown & ", " & vState & " " & vZip & "<br/>"
vCompany += "Telephone: " & vTelephone
@Html.Raw(vCompany)
End Code
和插入字段的值 - 例如状态字段
@Html.TextBox("inputCompanyState", vState, New With {.id = "inputCompanyState", .class = "form-control", .style = "max-width:80px;"})
现在,如果我想用下拉列表替换State字段并填充像
这样的DataTablePublic Function ReturnStateList() As DataTable
Try
Dim DT As New DataTable
With DT.Columns
.Add("AB", GetType(String))
.Add("Name", GetType(String))
End With
With DT.Rows
.Add("0", "Select State")
.Add("AL", "Alabama (AL)")
.Add("AK", "Alaska (AK)")
.Add("AZ", "Arizona (AZ)")
.Add("AR", "Arkansas (AR)")
.Add("CA", "California (CA)")
.Add("CO", "Colorado (CO)")
.Add("CT", "Connecticut (CT)")
.Add("DE", "Delaware (DE)")
.Add("DC", "District of Columbia (DC)")
.Add("FL", "Florida (FL)")
.Add("GA", "Georgia (GA)")
.Add("HI", "Hawaii (HI)")
.Add("ID", "Idaho (ID)")
.Add("IL", "Illinois (IL)")
.Add("IN", "Indiana (IN)")
.Add("IA", "Iowa (IA)")
.Add("KS", "Kansas (KS)")
.Add("KY", "Kentucky (KY)")
.Add("LA", "Louisiana (LA)")
.Add("ME", "Maine (ME)")
.Add("MD", "Maryland (MD)")
.Add("MA", "Massachusetts (MA)")
.Add("MI", "Michigan (MI)")
.Add("MN", "Minnesota (MN)")
.Add("MS", "Mississippi (MS)")
.Add("MO", "Missouri (MO)")
.Add("MT", "Montana (MT)")
.Add("NE", "Nebraska (NE)")
.Add("NV", "Nevada (NV)")
.Add("NH", "New Hampshire (NH)")
.Add("NJ", "New Jersey (NJ)")
.Add("NM", "New Mexico (NM)")
.Add("NY", "New York (NY)")
.Add("NC", "North Carolina (NC)")
.Add("ND", "North Dakota (ND)")
.Add("OH", "Ohio (OH)")
.Add("OK", "Oklahoma (OK)")
.Add("OR", "Oregon (OR)")
.Add("PA", "Pennsylvania (PA)")
.Add("RI", "Rhode Island (RI)")
.Add("SC", "South Carolina (SC)")
.Add("SD", "South Dakota (SD)")
.Add("TN", "Tennessee (TN)")
.Add("TX", "Texas (TX)")
.Add("UT", "Utah (UT)")
.Add("VT", "Vermont (VT)")
.Add("VA", "Virginia (VA)")
.Add("WA", "Washington (WA)")
.Add("WV", "West Virginia (WV)")
.Add("WI", "Wisconsin (WI)")
.Add("WY", "Wyoming (WY)")
End With
Return DT
Catch ex As Exception
' EmailError(ex)
Return Nothing
End Try
End Function
然后选择值&#39; vState&#39;我迷路了...
有什么建议吗?感谢
谢谢Shyju花时间添加该代码 - 它让我走上正轨。
@Html.DropDownList("AB", vProspects.States, New With {.class = "form-control"})
为我治好了。花了好几个小时试图弄清楚为什么它没有工作,直到我注意到我从DataTable填充后没有返回列表: - (
答案 0 :(得分:1)
这是解决方案的C#版本。将它转换为VB.NET
应该很简单确保您的View模型具有下拉列表的属性。一个用于下拉选项,另一个用于selectedItem。
例如,
public class AddUserVM
{
public string Name { set;get;}
public List<SelectListItem> States { set;get;}
public int SelectedState { set;get;}
public AddUserVM()
{
Cities=new List<SelectListItem>();
}
}
现在在您的GET操作方法中,填充视图模型的States集合并将其发送到视图。
public ActionResult AddUser()
{
var addUserVM = new AddUserVM();
addUserVM.States = GetStates();
return View(addUserVM);
}
private List<SelectListItem> GetStates()
{
var list = new List<SelectListItem>();
//Hard coded for demo. You may get the items
// from a datasource and add to the list
list.Add(new SelectListItem { Value = "1", Text = "MI" });
list.Add(new SelectListItem { Value = "2", Text = "OH" });
return list;
}
现在在你看来,
@model ReplaceYourNameSpaceHere.AddUserVM
@using (Html.BeginForm())
{
<p>
Name @Html.TextBoxFor(s=>s.Name)
</p>
<p>
State @Html.DropDownListFor(s=>s.SelectedState,Model.States,"Select")
</p>
<input type="submit" />
}
当用户提交表单时,您可以阅读SelectedState
属性以获取用户的下拉选项。
[HttpPost]
public ActionResult AddUser(AddUserVM model)
{
if (ModelState.IsValid)
{
// get the selected state
int stateId = model.SelectedState;
// to do : Save and redirect (PRG pattern) instead of this demo code below
return View("Results", model);
}
//Reload the Cities collection again
model.States = GetStates();
return View(model);
}