C# 带有下拉列表的Asp.Net MVC,然后选择ListItem Assistance
我正在尝试构建Dropdownlist,但正在与Html.Dropdownlist渲染进行斗争 我有一门课:C# 带有下拉列表的Asp.Net MVC,然后选择ListItem Assistance,c#,asp.net-mvc,selectlistitem,C#,Asp.net Mvc,Selectlistitem,我正在尝试构建Dropdownlist,但正在与Html.Dropdownlist渲染进行斗争 我有一门课: public class AccountTransactionView { public IEnumerable<SelectListItem> Accounts { get; set; } public int SelectedAccountId { get; set; } } 公共类AccountTransactionView { 公共IEnumerab
public class AccountTransactionView
{
public IEnumerable<SelectListItem> Accounts { get; set; }
public int SelectedAccountId { get; set; }
}
公共类AccountTransactionView
{
公共IEnumerable帐户{get;set;}
public int SelectedAccountId{get;set;}
}
这基本上是我目前的视图模型。帐户列表,以及用于返回所选项目的属性
在我的控制器中,我像这样准备好数据:
public ActionResult AccountTransaction(AccountTransactionView model)
{
List<AccountDto> accounts = Services.AccountServices.GetAccounts(false);
AccountTransactionView v = new AccountTransactionView
{
Accounts = (from a in accounts
select new SelectListItem
{
Text = a.Description,
Value = a.AccountId.ToString(),
Selected = false
}),
};
return View(model);
}
公共操作结果AccountTransaction(AccountTransactionView模型)
{
List accounts=Services.AccountServices.GetAccounts(false);
AccountTransactionView v=新建AccountTransactionView
{
Accounts=(来自Accounts中的a)
选择新的SelectListItem
{
Text=a.说明,
Value=a.AccountId.ToString(),
所选=错误
}),
};
返回视图(模型);
}
现在的问题是:
然后,我尝试在我的视图中构建下拉列表:
<%=Html.DropDownList("SelectedAccountId", Model.Accounts) %>
我得到以下错误:
具有键“SelectedAccountId”的ViewData项的类型为“System.Int32”,但其类型必须为“IEnumerable”
为什么它要我归还所有的物品?我只需要选定的值。我应该如何执行此操作?您有一个视图为强类型的视图模型=>使用强类型帮助程序:
<%= Html.DropDownListFor(
x => x.SelectedAccountId,
new SelectList(Model.Accounts, "Value", "Text")
) %>
步骤1:您的模型类
public class RechargeMobileViewModel
{
public string CustomerFullName { get; set; }
public string TelecomSubscriber { get; set; }
public int TotalAmount { get; set; }
public string MobileNumber { get; set; }
public int Month { get; set; }
public List<SelectListItem> getAllDaysList { get; set; }
// Define the list which you have to show in Drop down List
public List<SelectListItem> getAllWeekDaysList()
{
List<SelectListItem> myList = new List<SelectListItem>();
var data = new[]{
new SelectListItem{ Value="1",Text="Monday"},
new SelectListItem{ Value="2",Text="Tuesday"},
new SelectListItem{ Value="3",Text="Wednesday"},
new SelectListItem{ Value="4",Text="Thrusday"},
new SelectListItem{ Value="5",Text="Friday"},
new SelectListItem{ Value="6",Text="Saturday"},
new SelectListItem{ Value="7",Text="Sunday"},
};
myList = data.ToList();
return myList;
}
}
@model MvcVariousApplication.Models.RechargeMobileViewModel
@{
ViewBag.Title = "Contact";
}
@Html.LabelFor(model=> model.CustomerFullName)
@Html.TextBoxFor(model => model.CustomerFullName)
@Html.LabelFor(model => model.MobileNumber)
@Html.TextBoxFor(model => model.MobileNumber)
@Html.LabelFor(model => model.TelecomSubscriber)
@Html.TextBoxFor(model => model.TelecomSubscriber)
@Html.LabelFor(model => model.TotalAmount)
@Html.TextBoxFor(model => model.TotalAmount)
@Html.LabelFor(model => model.Month)
@Html.DropDownListFor(model => model.Month, new SelectList(Model.getAllDaysList, "Value", "Text"), "-Select Day-")
步骤3:按如下方式填写视图下拉列表
public class RechargeMobileViewModel
{
public string CustomerFullName { get; set; }
public string TelecomSubscriber { get; set; }
public int TotalAmount { get; set; }
public string MobileNumber { get; set; }
public int Month { get; set; }
public List<SelectListItem> getAllDaysList { get; set; }
// Define the list which you have to show in Drop down List
public List<SelectListItem> getAllWeekDaysList()
{
List<SelectListItem> myList = new List<SelectListItem>();
var data = new[]{
new SelectListItem{ Value="1",Text="Monday"},
new SelectListItem{ Value="2",Text="Tuesday"},
new SelectListItem{ Value="3",Text="Wednesday"},
new SelectListItem{ Value="4",Text="Thrusday"},
new SelectListItem{ Value="5",Text="Friday"},
new SelectListItem{ Value="6",Text="Saturday"},
new SelectListItem{ Value="7",Text="Sunday"},
};
myList = data.ToList();
return myList;
}
}
@model MvcVariousApplication.Models.RechargeMobileViewModel
@{
ViewBag.Title = "Contact";
}
@Html.LabelFor(model=> model.CustomerFullName)
@Html.TextBoxFor(model => model.CustomerFullName)
@Html.LabelFor(model => model.MobileNumber)
@Html.TextBoxFor(model => model.MobileNumber)
@Html.LabelFor(model => model.TelecomSubscriber)
@Html.TextBoxFor(model => model.TelecomSubscriber)
@Html.LabelFor(model => model.TotalAmount)
@Html.TextBoxFor(model => model.TotalAmount)
@Html.LabelFor(model => model.Month)
@Html.DropDownListFor(model => model.Month, new SelectList(Model.getAllDaysList, "Value", "Text"), "-Select Day-")
谢谢@Darin Dimitrov——这很有效——但我不确定我的尝试到底出了什么问题。您能解释一下吗?@cdotlister,不确定,但问题可能与您将错误的模型传递给控制器操作有关。谢谢。我在发布后发现了。我正在尝试使用Selected复制相同的代码,但我无法选择默认值。对于一个项目,Select的值显然是=true,但它没有在屏幕上显示为Select。Mine在cshtml中给出了一个错误,即它无法将方法组转换为IEnumerable。