model.id" 更改为 "model => model.Supplierid" 时出现以下错误 "The parameter 'expression' must eva-6ren">
gpt4 book ai didi

c# - 在 MVC4 中绑定(bind) ListBoxFor 控件时出错

转载 作者:太空狗 更新时间:2023-10-29 19:57:14 29 4
gpt4 key购买 nike

当我将 "model => model.id" 更改为 "model => model.Supplierid" 时出现以下错误

"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."

请看下面的代码

//这是我的模型类

public class clslistbox{
public int id { get; set; }
public int Supplierid { get; set; }
public List<SuppDocuments> lstDocImgs { get; set; }

public class SuppDocuments
{
public string Title { get; set; }
public int documentid { get; set; }
}
public List<SuppDocuments> listDocImages()
{
List<SuppDocuments> _lst = new List<SuppDocuments>();
SuppDocuments _supp = new SuppDocuments();
_supp.Title = "title";
_supp.documentid = 1;
_lst.Add(_supp);
return _lst;
}
}

//这是我的 Controller

    [HttpGet]
public ActionResult AddEditSupplier(int id)
{

clslistbox _lst = new clslistbox();
_lst.lstDocImgs= _lst.listDocImages();
return View(_lst);
}

//这是我绑定(bind)列表框的 View

@model clslistbox
@using (Html.BeginForm("AddEditSupplier", "Admin", FormMethod.Post))
{
@Html.ListBoxFor(model => model.id, new SelectList(Model.lstDocImgs, "documentid", "title"))
}

谁能看出其中的原因?

最佳答案

我认为此处表达式中属性的更改是转移注意力 - 在任何一种情况下都不起作用。

更新

但是,请参阅我的答案末尾的一些可能不必要的详细说明,说明为什么您在第一轮没有遇到错误。

结束更新

您正在使用 ListBoxFor - 用于为用户提供多种选择功能 - 但您正试图将其绑定(bind)到 int属性 - 不支持多选。 (它至少需要是 IEnumerable<T> 才能在 MVC 中默认将列表框绑定(bind)到它)

我认为你的意思是使用 DropDownListFor - 即显示一个只能从中选择一个的项目列表?

如果您实际上是在列表框中寻找单选语义,那么在 MVC 中要做到这一点就比较棘手,因为它的 Html 帮助程序完全围绕用于多选的列表框进行调整。 SO 上的其他人问了一个关于如何让下拉列表看起来像列表框的问题:How do I create a ListBox in ASP.NET MVC with single selection mode? .

或者您可以自己为这样的列表框生成 HTML。

(更新)- 可能不必要的详细说明(!)

你第一次没有得到异常的原因可能是因为 id 没有值在ModelState生成 HTML 时。这是感兴趣的反射 MVC 源(来自 SelectExtensions.SelectInternal)(末尾的 GetSelectListWithDefaultValue 调用是异常的来源):

object obj = 
allowMultiple ? htmlHelper.GetModelStateValue(fullHtmlFieldName, typeof(string[])) :
htmlHelper.GetModelStateValue(fullHtmlFieldName, typeof(string));
if (!flag && obj == null && !string.IsNullOrEmpty(name))
{
obj = htmlHelper.ViewData.Eval(name);
}
if (obj != null)
{
selectList =
SelectExtensions.GetSelectListWithDefaultValue(selectList, obj, allowMultiple);
}

首先注意控制变量allowMultiple在您的情况下是正确的,因为您调用了 ListBoxFor . selectListSelectList您创建并作为第二个参数传递。 MVC(不幸的是在某些情况下)做的一件事是使用 ModelState修改重新显示 View 时传递的选择列表,以确保在 ModelState 中设置的值通过 POST 在重新加载 View 时重新选择(这在页面验证失败时很有用,因为您不会将值从 ModelState 复制到基础模型,但页面仍应显示这些值已被选中)。

因此,正如您在第一行看到的,您传递的表达式/字段的模型当前值已从模型状态中取出;作为字符串数组或字符串。如果失败(返回 null ),那么它会再次执行表达式(或类似表达式)以获取模型值。如果它从那里得到一个非空值,它调用 SelectExtensions.GetSelectListWithDefaultValue .

正如我所说 - 在 Id 的两种情况下,您尝试做的最终都不会奏效。或 SupplierId (因为它们需要是 IEnumerable )但是我相信这 ModelState -> Eval当您使用 Id 时,进程产生空值,所以得到“调整”的过程SelectList被跳过 - 因此不会引发异常。当您使用 SupplierId 时,情况并非如此。因为我敢打赌 ModelState 中有一个值那时,或 ViewData.Eval成功获取整数值。

不抛出异常工作不同!

结束更新

关于c# - 在 MVC4 中绑定(bind) ListBoxFor 控件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14275538/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com