我正在尝试执行以下操作:
@Html.DropDownListFor(model => model.HPA, new SelectList(Categories.Types, "TupleId", "Name"), new { @class = "form-control" })
model.HPA包含以下内容:
public List<HPADTO> HPAs { get; set; }
public Tuple<int, int> HPA
{
get
{
// DO NOT USE.
return Tuple.Create<int, int>(-1, -1);
}
set
{
var hPA = new HPADTO();
hPA.PersonCategoryId = value.Item1;
hPA.PersonId = value.Item2;
HPAs.Add(hPA);
}
}
在视图中正确显示:
<select id="id" class="form-control" name="HPA">
<option value="(1, 1)"></option>
<option value="(1, 5)"></option>
</select>
但它引发了&#34; NullReferenceException&#34;在回复后作为&#34;值&#34;在HPA.set中为null。
控制器的代码(在评论中要求):
public ActionResult Add([Bind(Include = "HPA")] ModelClass model)
{
//This point is never reached due the NullReferenceException error...
if (ModelState.IsValid)
{
// I would persist it here.
}
return View();
}
错误在哪里或我应该如何正确处理这种情况?