0
我正在創建一個空的子拉框,它根據其父項動態填充。當表單被引用時,屬性Platform
始終爲空。爲什麼?將字符串屬性綁定到動態填充的下拉列表中並沒有設置值
這裏是我的ViewModel:
public class SkillsViewModel
{
public int SkillId { get; set; }
public string Platform { get; set; }
}
這是我的觀點:
@using (@Html.BeginForm("AddSkill", "AddSkills"))
{
@Html.DropDownListFor(m => m.SkillId,
new SelectList(ViewBag.SkillsFound, "Id", "SkillName"), "Select skill",
new { @class = "form-control", @onchange = "SkillSelected()" })
@Html.DropDownListFor(m => m.Platform,
new SelectList(Enumerable.Empty<SelectListItem>()),
new { @class = "form-control" })
<input type="submit" name="submit" value="Add skill" class="btn btn-success" />
}
和控制器:
[HttpPost]
public ActionResult AddSkill(SkillsViewModel model)
{
// model.Platform is always null
// to be implemented...
return RedirectToAction("Index");
}
這裏是填充dropdown.It的紙條作品的權利 - 在下拉列表中添加字符串列表
編輯:
這裏是返回字符串列表控制器。
public class GetSkillPlatformsController : Controller
{
[HttpPost]
public ActionResult Index(int skillId)
{
var Db = new DbContext();
List<Platform> PlatformsFound = Db.Platforms.Where(pl => pl.PlatformSkill.Id == skillId).ToList();
List<string> PlatformsNames = new List<string>();
foreach(Platform pl in PlatformsFound)
{
PlatformsNames.Add(pl.PlatformName);
}
return Json(PlatformsNames);
}
}
您需要顯示腳本,在其中填充第二個下拉列表 –
那麼它的工作原理ks正確,並用字符串列表填充下拉列表,所以我認爲問題不應該在那裏。 –
當然它:)。你添加選項的代碼是錯誤的 - 如果'data'是一個字符串,那麼它不能有'data.Id'屬性(因此每個選項都有一個'null'值)。你需要顯示控制器的方法,返回的數據生成的選項 –