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
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
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());
model.AvailableModel = getAllInstituteNameList().Except(model.RequestedModel).ToList();
}
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);
}
}
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;
}
}
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)) %>
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…