【发布时间】:2015-05-11 07:18:32
【问题描述】:
我正在尝试将我的 dropdownlist 菜单中的选定项目保存在一个 MSSQL 表中,其中该菜单是从另一个 MSSQL 表填充的。但是每当我运行我的项目时,我都会遇到编译错误,
CS1502:'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, string, string)' 的最佳重载方法匹配有一些无效参数
这是我的代码,
查看
@using (Html.BeginForm("BoxManager", "Home"))
{
@Html.ValidationSummary(true)
<div class="editor-label">
<strong>Full Name</strong>
</div>
<div class="editor-field">
@Html.DropDownListFor(a => a.Boxes.clientname, new SelectList(Model.Clients, "fullname", "fullname"))
@Html.ValidationMessageFor(a => a.Boxes.clientname)
</div>
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
型号
public class BoxManagement
{
public BoxInfo Boxes { get; set; }
public IEnumerable<BoxInfo> BoxCollection { get; set; }
public ClientInfo Clients { get; set; }
}
控制器
public ActionResult BoxManager()
{
return View();
}
[HttpPost]
public ActionResult BoxManager(BoxManagement BoxModel)
{
if (ModelState.IsValid)
{
var AddBox = BoxModel.Boxes;
db.BoxInfoes.Add(AddBox);
db.SaveChanges();
return RedirectToAction("BoxPanel");
}
return View(BoxModel.Boxes);
}
我在这里做错了吗?如何在我的BoxInfo 模型中保存下拉列表中的选定项?非常需要这个帮助。 Tnx。
【问题讨论】:
-
尝试类似的方法:@Html.DropDownListFor(a => a.Boxes.clientname, (List
)Model.Clients, "fullname", null) -
检查您的数据源。您可能有 av 键和值集合。在你的情况下,“钥匙”可能是别的东西。 @Html.DropDownListFor(a => a.Boxes.clientname, new SelectList(Model.Clients, "Key", "clientname")
-
除了下面的答案之外,您的代码中还有其他错误。如果
ModelState无效,那么它应该是return View(BoxModel);,您需要重新分配BoxCollection的值
标签: c# asp.net sql-server asp.net-mvc razor