Html.ListBoxFor误差问题asp.mvc 3 [英] Html.ListBoxFor error problem asp.mvc 3

查看:216
本文介绍了Html.ListBoxFor误差问题asp.mvc 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的code这样的事情,我得到错误:异常详细信息:System.ArgumentException:值不能为null或空。
参数名:名称。我究竟做错了什么?感谢您的帮助。

I have something like this in my code and I am getting error: Exception Details: System.ArgumentException: Value cannot be null or empty. Parameter name: name . What am I doing wrong ? Thanks for help

@model IEnumerable的< NHibernateFluentProject.Patient>

@ Html.ListBoxFor(型号=>的模式,新的SelectList(型号,ID,名字));

推荐答案

@ Html.ListBoxFor用于您的强类型视图模型。这将有助于绑定到你的财产。第一部分将采取一个lambda前pression为单个项目作为默认seleced你的列表框,第二部分将采取项目集合来dispaly所有的列表框项目。
例如:你有以下两类

@Html.ListBoxFor is used for your strong typed viewmodel. which could help to bind to your property. First part will take a lambda expression for a single item as a default seleced for your listbox, second part will take the item collections to dispaly all the listbox items. For example: you have following two classes.

public class HospitalViewModel
{
    public string SelectedPatient { get; set; }
    public IEnumerable<Patient> AllPatients { get; set; }
}
public class Patient
{
    public int Id { get; set; }
    public string FirstName { get; set; }
}

从您查看,你应该做这样的事情。

From you view, you should do something like

@model HospitalViewModel

@Html.ListBoxFor(model => model.SelectedPatient, new SelectList(Model.AllPatients,"Id", "FirstName"));

或者如果你只希望将所有的病人绑定到一个列表框,然后用Html.ListBox代替

OR if you only want to bind all your patients to a listbox, then use Html.ListBox instead

@model IEnumerable<Patient>

@Html.ListBox("ListBoxName", new SelectList(Model,"Id", "FirstName"));

这篇关于Html.ListBoxFor误差问题asp.mvc 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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