【发布时间】:2014-11-24 20:18:02
【问题描述】:
我想从表格中获取包含值的下拉列表。我有模特:
namespace MobileService.Models
{
public class Mobile
{
public int Id { get; set; }
public string Model { get; set;}
Public string EMEI { get; set ;}
public int MasterId { get; set; }
public List<Master> MasterList { get; set; }
}
public class Master
{
public int Id { get; set; }
public string Name { get; set; }
}
}
在控制器中我为此模型创建动作:
public ActionResult Create()
{
return View();
}
在视图中:
@model MobileService.Models.Mobile
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.Model)
@Html.EditorFor(model => model.EMEI )
// And here i want to display a dropdown list with all available masters
<button type="submit" class="btn btn-primary">Creat</button>
}
但它总是告诉我 Model.MasterList 是 null 。但为什么?在模型中,我告诉从另一个模型获取列表(public List MasterList { get; set; })。为什么是空的?
【问题讨论】:
-
向我们展示你想加载模型的控制器代码..
-
C# MVC get values from dropdownlist 太简单了
GOOGLE SEARCH可以Yield -
Model.MasterList 为 null 。但是为什么呢? - 是什么让你认为它不会是?
-
试试this topic。有一个关于如何创建
DropDownList的解释。 -
但它总是告诉我 Model.MasterList 为空 目前,根据您的代码,您没有向 View 返回任何内容:
return View() // it's EMPTY。因此,您的模型为空,即使视图需要Mobile。稍后,查找SelectList和DropDownList。 SelectList 可能很棘手,但一旦你理解了它,创建DropDonwList甚至更好DropFownListFor很容易
标签: c# asp.net asp.net-mvc asp.net-mvc-4