MVC ListBoxFor引发"值不能为空"例外 [英] MVC ListBoxFor raises "value cannot be null" exception

查看:143
本文介绍了MVC ListBoxFor引发"值不能为空"例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Html.ListBoxFor助手显示一个列表框,并返回选定的标识。是否与不是一个字符串dataValueField问题?

如果包含在模型中选择列表的使用整数作为dataValueField然后我得到一个值不能为空 - 参数名:源时,列表中的视图显示出现了异常。

如果该ID更改为一个字符串,然后一切工作和所选择的ID传回视图。

任何想法?

下面是控制器(基于降低新项目)

 命名空间Mvc2.Controllers
{
    公共类视图模型
    {
        公众诠释TestId {搞定;组; } //如果这是一个字符串,它的工作原理确定
        公众的SelectList的ListData {搞定;组;}
    }    [的HandleError]
    公共类HomeController的:控制器
    {
        公众的ActionResult指数()
        {
            VAR模型=新视图模型();
            model.TestId = 1; // code拉撒路的意见后修正            VAR LST =新[] {{新ID = 1,名称=猫},{新ID = 2,名称=狗}};
            model.ListData =新的SelectList(LST,ID,姓名);            返回视图(TestView模型);
        }        公众的ActionResult TestSubmit(视图模型returnedModel)
        {
            INT I = 99; //这里突破 - returnedModel具有正确TestId为字符串时宣布
        }
    }
}

这里是观 - 对ListBoxFor线崩溃

 <使用(Html.BeginForm(TestSubmit,家)){%GT%;    <%= Model.TestId%GT;< BR />
    &所述;%= Html.ListBoxFor(M => m.TestId,Model.ListData)%GT;    < BR />
    <输入类型=提交值=保存/><%}%GT;


解决方案

回答我的问题;

我的意见,这可能是它正在等待被固定的,因为我在RC2和MVC 1得到它的错误unconviced(我复制了code回到一个项目,在该版本中)。
无论如何,我已经实现了一个变通现在这是: -

(一)添加ID的虚拟字符串版本模型(TestId)

 公共类视图模型
{
    公共字符串TestId {搞定;组; } //虚拟ID作为一个字符串
    公开名单< D​​ataToShow>数据{搞定;组; }
    公众的SelectList的ListData {搞定;组;}
}

(二)显示列表中,但检索值作为虚拟TestId - 注意,列表中仍然转储数据值作为整数

 <%= Html.ListBoxFor(M = GT; m.TestId,Model.ListData)%GT;

(三)虚拟字符串值复制到它应有的位置的整数中的作用

 公众的ActionResult TestSubmit(视图模型returnedModel)
{
    MyModel.DataId = Int32.Parse(returnedModel.TestId);

希望这是一些帮助。

I am trying to use the Html.ListBoxFor helper to show a list box and return the selected Id. Is there a problem with the dataValueField not being a string?

If the SelectList contained in the model uses integers as the dataValueField then I get a "Value cannot be null - Parameter name: Source" exception raised when the list is rendered in the view.

If the Id is changed to a string then everything works and the selected Id is passed back to the view.

Any ideas?

Here is the controller (based on a cut down new project)

namespace Mvc2.Controllers
{
    public class ViewModel
    {
        public int TestId { get; set; } // if this is a string it works ok
        public SelectList ListData {get; set;}
    }

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new ViewModel();
            model.TestId = 1; // code corrected after Lazarus' comment

            var lst = new[] { new { Id = 1, Name = "cat" }, new { Id = 2, Name = "dog" } };
            model.ListData = new SelectList(lst, "Id", "Name");

            return View("TestView", model);
        }

        public ActionResult TestSubmit(ViewModel returnedModel)
        {
            int i = 99; // break here - returnedModel has correct TestId when declared as string
        }
    }
}

here is the View - crashes on the ListBoxFor line

<%using (Html.BeginForm("TestSubmit", "Home")) { %>

    <%=Model.TestId %><br />
    <%=Html.ListBoxFor(m => m.TestId, Model.ListData) %>

    <br />
    <input type="submit" value="Save" />

<%} %>

解决方案

Answering my own question;

I am unconviced by the comments that this might be a bug which is waiting to be fixed because I get it in RC2 and in MVC 1 (I copied the code back to a project in that release). Anyway I have implemented a work around for now which is to:-

(a) Add a dummy string version of the Id to the model (TestId)

public class ViewModel
{
    public string TestId { get; set; } // dummy Id as a string
    public List<DataToShow> Data { get; set; }
    public SelectList ListData {get; set;}
}

(b) Display the list but retrieve the value as the dummy TestId - note that the list still dumps the data values as integers!

<%=Html.ListBoxFor(m => m.TestId, Model.ListData) %>

(c) Copy the dummy string value into its proper integer location in the action

public ActionResult TestSubmit(ViewModel returnedModel)
{
    MyModel.DataId = Int32.Parse(returnedModel.TestId);

Hope this is of some Help.

这篇关于MVC ListBoxFor引发&QUOT;值不能为空&QUOT;例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆