0
您好,我正在學習編程ASP.NET MVC風格。我試圖用我的PresentationModel填充組合框,它不知何故保持空白。ASP.NET MVC PresentationModel綁定到ComboBoxes保持空
我使用的ASP標籤助手:
視圖(Index.cshtml)
@model Week3_oef2_ITPro.PresentationModel.PMRegistration
<h2>New Registration</h2>
<h4>Registration</h4>
<form asp-controller="Register" asp-action="" method="post">
<table>
<tr>
<td>Organization</td>
<td class="form-group">
<select asp-for="OrgId.Id" asp-items="@Model.Org" class="form-group" />
</td>
</tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
PresentationModel(PMRegistration.cs)
public class PMRegistration
{
public Organization OrgId { get; set; }
public List<SelectListItem> Org { get; set; }
}
模型(Organization.cs)
public class Organization
{
public int Id { get; set; }
public string Name { get; set; }
}
數據(其中所有的對象都被初始化)
public class Data
{
private static List<Session> sessions = new List<Session>();
private static List<Organization> organizations = new List<Organization>();
private static List<Device> devices = new List<Device>();
static Data()
{
organizations.Add(new Organization() { Id = 1, Name = "Howest" });
organizations.Add(new Organization() { Id = 2, Name = "Vives" });
organizations.Add(new Organization() { Id = 3, Name = "HoGent" });
organizations.Add(new Organization() { Id = 4, Name = "HoLimburg" });
organizations.Add(new Organization() { Id = 4, Name = "De blauwe smurfen" });
devices.Add(new Device() { Id = 1, Name = "Laptop" });
devices.Add(new Device() { Id = 2, Name = "Tablet" });
devices.Add(new Device() { Id = 3, Name = "Apple Watch" });
}
public static List<Device> GetDevices()
{
return devices;
}
public static List<Organization> GetOrganizations()
{
return organizations;
}
}
控制器(RegisterController.cs)
public class RegisterController : Controller
{
// GET: /<controller>/
[HttpGet]
public IActionResult Index()
{
PMRegistration pm = new PMRegistration();
pm.OrgId = new Organization();
pm.Org = ConverToListItems(Data.GetOrganizations());
return View(pm);
}
#region methodes
private List<SelectListItem> ConverToListItems(List<Organization> data)
{
List<SelectListItem> items = new List<SelectListItem>();
foreach (var item in data)
{
items.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
}
return items;
}
#endregion
}
這裏是因爲你所傳遞'PMRegistration'類的對象'Index'操作方法的代碼? – Shyju
忘記顯示我的控制器,一秒 –
@Shyju它現在加了,我忘了對不起 –