对象引用未设置为ListBox上的对象实例

时间:2014-04-30 05:32:32

标签: c# asp.net asp.net-mvc asp.net-mvc-4 listbox

我正在研究双列表框并通过以下文章将项目从Listbox1移动到asp.net MVC中的Listbox1 ...... ASP.NET MVC 2 Basics - Working with ListBoxes

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

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; }
    }

控制器:

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
}

查看:

<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>
<% }%>

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

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

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

错误是

  

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

我不知道任何人都可以帮助我解决问题......

0 个答案:

没有答案