【问题标题】:Save selected item from dropdownlist in asp.net mvc从 asp.net mvc 的下拉列表中保存所选项目
【发布时间】: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


【解决方案1】:

这里的原因是您传递的是ClientInfo 类型的对象,而不是IEnumerable&lt;&gt; 所需的类型。您正在将以下行传递到您的下拉列表中:

new SelectList(Model.Clients, "fullname", "fullname")

Clients 属于 ClientInfo 类型,而不是 IEnumerable。您需要将其更改为代表 IEnumerabletype 的内容。

一个例子是:

Model.BoxCollection

这是IEnumerable,根据您的型号。

更新:

这应该是您的型号代码:

public class BoxManagement
{
    public BoxInfo Boxes { get; set; }
    public IEnumerable<BoxInfo> BoxCollection { get; set; }
    public IEnumerable<ClientInfo> Clients { get; set; }
}

这应该是你的控制器代码:

var ClientCollection = db.ClientsInfo.Select(..conditions..).ToList();
Model.Clients = ClientCollection;
return View(Model);

所以基本上,只需将 Clients 对象与您的查询绑定到 IEnumerable 并将该模型传递给您的视图。这就是我们所需要的。

【讨论】:

  • 在我的模型中,我使用了public IEnumerable&lt;ClientInfo&gt; Clients { get; set; } 而不是public ClientInfo Clients { get; set; }。现在我收到了这个错误,System.ArgumentNullException: Value cannot be null.。知道发生了什么吗?
  • 那是因为您需要先将数据绑定到您的Clients 对象中,然后再将其发送到视图。
  • Tnx 但请解释更多。
  • 显示获取IEnumerable&lt;ClientInfo&gt; Clients数据的代码
  • 很高兴它对您有所帮助。谢谢:)
【解决方案2】:

您在SelectList 声明中使用了错误的字段,您使用的是Client,但它不应该是Boxes,因为那是项目列表。 SelectList 构造函数中的第一项必须是 IEnumerable

如果你说的是盒子,那么试试:

@Html.DropDownListFor(a => a.Boxes.clientname, new SelectList(Model.BoxCollection, "fullname", "fullname"))

【讨论】:

  • 不,我正在使用需要填充下拉列表的客户端模型。现在,在我的模型中,我使用了public IEnumerable&lt;ClientInfo&gt; Clients { get; set; } 而不是public ClientInfo Clients { get; set; }。现在我收到了这个错误,System.ArgumentNullException: Value cannot be null.。知道发生了什么吗?
  • @SinOscuras 啊,对了,听起来你的收藏在那个时候是null。确保填充它。
  • 但我的客户表中有数据。那里没有空值。
  • @SinOscuras 听起来好像没有被退回。你能发布检索客户端数据的代码吗?
  • @SinOscuras 是的,它需要是这样的。您不会在 HttpGet 方法中填充模型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
相关资源
最近更新 更多