对象引用未设置为ListBox上的对象实例 [英] Object reference not set to an instance of an object on ListBox

查看:85
本文介绍了对象引用未设置为ListBox上的对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用双列表框并通过以下文章将项目从Listbox1移动到asp.net MVC中的Listbox1 ... ASP.NET MVC 2基础知识 - 使用ListBoxes [ ^ ]



我有一个名为Database1.mdf的数据库包含表格,模型和我已经在表格代码中添加了一些数据,模型就是这样...



Model.cs



I am working On Dual ListBox and Moving Items From Listbox1 to Listbox1 in asp.net MVC by Following this Article… ASP.NET MVC 2 Basics - Working with ListBoxes[^]

I have a database Named Database1.mdf the contain the table i.e. Model and I have added some data into that table code for model is like that…

Model.cs

public class Model
    {
public int Id { get; set; }
public string Name { get; set; }
public string Class { get; set; }
    }







Model1.context.cs < br $>





Model1.context.cs

public class Database1Entities : DbContext
    {
public Database1Entities()
            : base("name=Database1Entities")
        {
        }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
thrownew UnintentionalCodeFirstException();
        }

public DbSet<Model> Models { get; set; }
    }





ViewModel.cs





ViewModel.cs

public class ViewModel
    {
public List<Model> AvailableModel { get; set; }
public List<Model> RequestedModel { get; set; }

public int[] AvailableSelected { get; set; }
public int[] RequestedSelected { get; set; }

public string SavedRequested { get; set; }
    }







控制器:







Controller:


public class HomeController : Controller
    {
Database1Entities db = new Database1Entities();

        [NonAction]
public List<Model> getAllInstituteNameList()
        {
return (from m in db.Models select m).ToList();
        }


        [HttpGet]
public ActionResult Index()
        {
ViewModel model = new ViewModel{ AvailableModel =getAllInstituteNameList() , RequestedModel = new List<Model>() };
return View();

        }


        [HttpPost]
public ActionResult Index(ViewModel model,string add,string remove,string send)
        {
ModelState.Clear();
RestoreSavedState(model);

if (!string.IsNullOrEmpty(add))
AddModels(model);

else if (!string.IsNullOrEmpty(remove))
RemoveModels(model);

SaveState(model);
return View(model);
        }


#regionSupportFuncs
private void Validate(ViewModel model)
        {
if (string.IsNullOrEmpty(model.SavedRequested))
ModelState.AddModelError("", "You haven't selected any presents!");
        }

void SaveState(ViewModel model)
        {
model.SavedRequested = string.Join(",", model.RequestedModel.Select(p =>p.Id.ToString()).ToArray());

////Available Models = All - Requested
model.AvailableModel = getAllInstituteNameList().Except(model.RequestedModel).ToList();
        }


//RestoreSavedState
void RestoreSavedState(ViewModel model)
        {

model.RequestedModel = new List<Model>();

if (!string.IsNullOrEmpty(model.SavedRequested))
            {
string[] modelids = model.SavedRequested.Split(',');
var mod = getAllInstituteNameList().Where(p =>modelids.Contains(p.Id.ToString()));
model.RequestedModel.AddRange(mod);
            }
        }

//AddModels
void AddModels(ViewModel model)
        {
if (model.AvailableSelected != null)
            {
var mods = getAllInstituteNameList().Where(p =>model.AvailableSelected.Contains(p.Id));
model.RequestedModel.AddRange(mods);
model.AvailableSelected = null;
            }
        }

//RemoveModels

void RemoveModels(ViewModel model)
        {

if (model.RequestedSelected != null)
            {
model.RequestedModel.RemoveAll(p =>model.RequestedSelected.Contains(p.Id));

            }
        }

#endregion
}





查看:





View:

<h2>Index</h2>
<%using(Html.BeginForm()) {%>
<div>
<hr/>
<table>
<thead>
<tr>
<th>
                       Available
</th>

<th>

</th>

<th>
                       Requested
</th>
</tr>
</thead>

<tbody>
<tr>
<td>
<%:Html.ListBoxFor(model =>model.AvailableSelected, newMultiSelectList(Model.AvailableModel, "Id", "Name", Model.AvailableSelected))%>
</td>

<td>
<inputid="add"name="add"type="submit"value=">>"/>
<br/>
<inputid="remove"name="remove"type="submit"value="<<"/>
</td>

<td>
<%:Html.ListBoxFor(model=>model.RequestedSelected,newMultiSelectList(Model.RequestedModel,"Id","Name",Model.RequestedSelected)) %>
</td>
</tr>
</tbody>
</table>
<br/>

<hr/>
</div>
<% }%>







运行此之后,它会在这些行上显示错误






After Running this it gives Error on those lines

<%:Html.ListBoxFor(model =>model.AvailableSelected, newMultiSelectList(Model.AvailableModel, "Id", "Name", Model.AvailableSelected))%>

And

<%:Html.ListBoxFor(model=>model.RequestedSelected,newMultiSelectList(Model.RequestedModel,"Id","Name",Model.RequestedSelected)) %>





和错误是


对象引用未设置为对象的实例。



我没有得到什么proplem任何人都可以帮我解决它...



And Error is

Object reference not set to an instance of an object.

I don’t getting whats the proplem can anyone help me to solve it…

推荐答案

在调试器中运行你的代码,看看你在哪里得到错误,然后移动你的鼠标在线和在工具提示中查看哪个部分为空,然后从那里向后工作以修复它。
Run your code in the debugger and see where you are getting the error, then move your mouse around the line and see which part is null in the tooltip and work backward from there to fix it.


这篇关于对象引用未设置为ListBox上的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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