我有以下 _CreateOrEdit 部分视图:-
@model TMS.ViewModels.RackJoin
@* This partial view defines form fields that will appear when creating and editing entities *@
@Html.AntiForgeryToken()
<div >
<span class="f">RU </span>
@Html.EditorFor(model => model.Rack.RU)
@Html.ValidationMessageFor(model => model.Rack.RU)
</div>
<div >
<span class="f">Rack Name </span>
@Html.Editor("assetName", (Model.Resource.RESOURCENAME == null ? String.Empty : Model.Resource.RESOURCENAME))
@Html.ValidationMessageFor(model => model.Resource.RESOURCENAME)
</div>
<div >
编辑现有记录时,该视图将正常工作,但如果它是一个新对象,我将收到以下异常:-
你调用的对象是空的。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
在视图内的以下代码上:-
@Html.Editor("assetName", (Model.Resource.RESOURCENAME == null ? String.Empty : Model.Resource.RESOURCENAME))
更新
调用上述视图的操作方法是:-
public ActionResult Create()
{
ViewBag.PossibleDataCenters = repository.AllDataCenter().OrderBy(a => a.Name).ToList() ;
ViewBag.PossibleZones = repository.AllZone().OrderBy(a => a.Name).ToList();
ViewBag.Products = repository.GetProducts("Rack").OrderBy(a => a.COMPONENTNAME).ToList();
return View();
}