Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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



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

XML
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:

XML
<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

SQL
<%: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)) %>


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…
Posted
Comments
Laiju k 30-Apr-14 1:55am    
Check whether the List has items
Member 10714619 30-Apr-14 3:20am    
i have added items

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900