在选项卡之间切换的空引用异常
Null Reference Exception which switching between tabs
在我的控制器中,我有这样的东西:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Index()
{
int treatmentId = (int)UserSession.GetValue(StateNameEnum.ID, "TreatmentID");
List<int> Ids = db.frames.Where(f => f.PlanId == treatmentId).Select(f => f.Id).ToList();
int Id = Ids[0];
PlanViewModel vm = new PlanViewModel();
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
if (info.IsReference == 1)
{
vm.ReferenceId = ReferenceTypeEnum.Proximal.ToString();
}
else
{
vm.ReferenceId = ReferenceTypeEnum.Distal.ToString();
}
return PartialView(vm);
}
在我看来我有这段代码:
@if (Model.ReferenceId == "Proximal") //Getting Null Reference Exception here
{
<span class="circle Plan-circle" style="background-color: rgb(255,0,0)"></span>
}
else if (Model.ReferenceId == "Distal")
{
<span class="circle Plan-circle" style="background-color: rgb(0,0,255)"></span>
}
在我看来,这就是我想用来检查 ReferenceId 是否设置为特定字符串并根据模型中的值更改视图的逻辑。
但是,我注意到虽然上述方法有时有效,但我注意到在选项卡之间切换时出现空异常。我想知道为什么我的模型为空,以及我如何从控制器中获取逻辑是否有意义?
使用我在控制器中设置的内容,如何检查模型何时在视图中设置为特定值?
编辑:
切换标签时调用的js文件:
ToggleTabSwitch: function (data) {
var tabSelector = $('#tab-selector'); // any id you input in the first div
tabSelector.on('click', '.newUI-left-selector', function () {
$(this).removeClass('-newUI-left-selector');
$(this).addClass('newUI-toggled-left-selector');
//left selector toggled.
$.ajax({
url: BrowserSide.Url.getFullUrl("DeformityPlanning/LoadDPPartialView"),
type: 'GET',
cache: false,
success: function (result) {
$('#selector-render').html(result);
BrowserSide.Plan.enableButtons();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
BrowserSide.AjaxLog.HandleAjaxCallFail(XMLHttpRequest, textStatus, errorThrown, "ajax error in Plan.js Load DP_form view", "");
}
});
var rightToggedSelector = $('.newUI-toggled-right-selector');
if (rightToggedSelector.length > 0) {
rightToggedSelector.removeClass('newUI-toggled-right-selector');
rightToggedSelector.addClass('newUI-right-selector');
}
});
tabSelector.on('click', '.newUI-right-selector', function () {
$(this).removeClass('newUI-right-selector');
$(this).addClass('newUI-toggled-right-selector');
// right selector toggled
$.ajax({
url: BrowserSide.Url.getFullUrl("DeformityPlanning/LoadAPPartialView"),
type: 'GET',
cache: false,
success: function (result) {
$('#selector-render').html(result);
btnList = $("button[data-val-btnName]");
BrowserSide.Plan.enableButtons();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
BrowserSide.AjaxLog.HandleAjaxCallFail(XMLHttpRequest, textStatus, errorThrown, "ajax error in Plan.js Load DP_formtwo view", "");
}
});
var rightToggedSelector = $('.newUI-toggled-left-selector');
if (rightToggedSelector.length > 0) {
rightToggedSelector.removeClass('newUI-toggled-left-selector');
rightToggedSelector.addClass('newUI-left-selector');
}
});
}
我如何 return 控制器中的局部视图:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult LoadDPPartialView()
{
return PartialView("DP_form");
}
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult LoadAPPartialView()
{
List<PlanViewModel.PlanViewParam> newItem = new List<PlanViewModel.PlanViewParam>();
return PartialView("DP_formtwo");
}
编辑#2:
尝试解决问题:
private void GetReferenceId()
{
int treatmentId = (int)UserSession.GetValue(StateNameEnum.ID, "TreatmentID");
PlanViewModel vm = new PlanViewModel();
List<int> Ids = db.frames.Where(f => f.PlanId == treatmentId).Select(f => f.Id).ToList();
int Id = Ids[0];
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
if (info.IsReference == 1)
{
vm.ReferenceId = ReferenceTypeEnum.Proximal.ToString();
}
else
{
vm.ReferenceId = ReferenceTypeEnum.Distal.ToString();
}
}
public ActionResult LoadDPPartialView()
{
GetReferenceId();
return PartialView("DP_form");
}
您为 Razor/CSHTML 展示的逻辑似乎不错。 它非常专门用于 display/presentation,因为它只是选择一种颜色,所以我认为它在视图中是合适的。 没有任何我会称之为业务逻辑的东西。
尽管它可以通过 ReferenceTypeToRgbColor
之类的辅助函数变得更干,并在样式中使用它,或者甚至更好地发出 class 并将您的样式放入 CSS .我也可能建议使用枚举或常量而不是字符串 "Proximal",但那是另一回事了。
@function string CircleTypeToCssClass(string circleType)
{
if (circleType == "Proximal")
{
return "proximal-circle";
}
else if (circleType == "Distal")
{
return "distal-circle";
}
else
return "";
}
<span class="circle Plan-circle @(CircleTypeToCssClass(Model.ReferenceId))"></span>
确定您的异常来自 Razor 视图而不是控制器?
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
如果结果集为空,.First() 将抛出异常。尝试使用 .FirstOrDefault() 代替
在我的控制器中,我有这样的东西:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Index()
{
int treatmentId = (int)UserSession.GetValue(StateNameEnum.ID, "TreatmentID");
List<int> Ids = db.frames.Where(f => f.PlanId == treatmentId).Select(f => f.Id).ToList();
int Id = Ids[0];
PlanViewModel vm = new PlanViewModel();
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
if (info.IsReference == 1)
{
vm.ReferenceId = ReferenceTypeEnum.Proximal.ToString();
}
else
{
vm.ReferenceId = ReferenceTypeEnum.Distal.ToString();
}
return PartialView(vm);
}
在我看来我有这段代码:
@if (Model.ReferenceId == "Proximal") //Getting Null Reference Exception here
{
<span class="circle Plan-circle" style="background-color: rgb(255,0,0)"></span>
}
else if (Model.ReferenceId == "Distal")
{
<span class="circle Plan-circle" style="background-color: rgb(0,0,255)"></span>
}
在我看来,这就是我想用来检查 ReferenceId 是否设置为特定字符串并根据模型中的值更改视图的逻辑。
但是,我注意到虽然上述方法有时有效,但我注意到在选项卡之间切换时出现空异常。我想知道为什么我的模型为空,以及我如何从控制器中获取逻辑是否有意义?
使用我在控制器中设置的内容,如何检查模型何时在视图中设置为特定值?
编辑:
切换标签时调用的js文件:
ToggleTabSwitch: function (data) {
var tabSelector = $('#tab-selector'); // any id you input in the first div
tabSelector.on('click', '.newUI-left-selector', function () {
$(this).removeClass('-newUI-left-selector');
$(this).addClass('newUI-toggled-left-selector');
//left selector toggled.
$.ajax({
url: BrowserSide.Url.getFullUrl("DeformityPlanning/LoadDPPartialView"),
type: 'GET',
cache: false,
success: function (result) {
$('#selector-render').html(result);
BrowserSide.Plan.enableButtons();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
BrowserSide.AjaxLog.HandleAjaxCallFail(XMLHttpRequest, textStatus, errorThrown, "ajax error in Plan.js Load DP_form view", "");
}
});
var rightToggedSelector = $('.newUI-toggled-right-selector');
if (rightToggedSelector.length > 0) {
rightToggedSelector.removeClass('newUI-toggled-right-selector');
rightToggedSelector.addClass('newUI-right-selector');
}
});
tabSelector.on('click', '.newUI-right-selector', function () {
$(this).removeClass('newUI-right-selector');
$(this).addClass('newUI-toggled-right-selector');
// right selector toggled
$.ajax({
url: BrowserSide.Url.getFullUrl("DeformityPlanning/LoadAPPartialView"),
type: 'GET',
cache: false,
success: function (result) {
$('#selector-render').html(result);
btnList = $("button[data-val-btnName]");
BrowserSide.Plan.enableButtons();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
BrowserSide.AjaxLog.HandleAjaxCallFail(XMLHttpRequest, textStatus, errorThrown, "ajax error in Plan.js Load DP_formtwo view", "");
}
});
var rightToggedSelector = $('.newUI-toggled-left-selector');
if (rightToggedSelector.length > 0) {
rightToggedSelector.removeClass('newUI-toggled-left-selector');
rightToggedSelector.addClass('newUI-left-selector');
}
});
}
我如何 return 控制器中的局部视图:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult LoadDPPartialView()
{
return PartialView("DP_form");
}
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult LoadAPPartialView()
{
List<PlanViewModel.PlanViewParam> newItem = new List<PlanViewModel.PlanViewParam>();
return PartialView("DP_formtwo");
}
编辑#2:
尝试解决问题:
private void GetReferenceId()
{
int treatmentId = (int)UserSession.GetValue(StateNameEnum.ID, "TreatmentID");
PlanViewModel vm = new PlanViewModel();
List<int> Ids = db.frames.Where(f => f.PlanId == treatmentId).Select(f => f.Id).ToList();
int Id = Ids[0];
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
if (info.IsReference == 1)
{
vm.ReferenceId = ReferenceTypeEnum.Proximal.ToString();
}
else
{
vm.ReferenceId = ReferenceTypeEnum.Distal.ToString();
}
}
public ActionResult LoadDPPartialView()
{
GetReferenceId();
return PartialView("DP_form");
}
您为 Razor/CSHTML 展示的逻辑似乎不错。 它非常专门用于 display/presentation,因为它只是选择一种颜色,所以我认为它在视图中是合适的。 没有任何我会称之为业务逻辑的东西。
尽管它可以通过 ReferenceTypeToRgbColor
之类的辅助函数变得更干,并在样式中使用它,或者甚至更好地发出 class 并将您的样式放入 CSS .我也可能建议使用枚举或常量而不是字符串 "Proximal",但那是另一回事了。
@function string CircleTypeToCssClass(string circleType)
{
if (circleType == "Proximal")
{
return "proximal-circle";
}
else if (circleType == "Distal")
{
return "distal-circle";
}
else
return "";
}
<span class="circle Plan-circle @(CircleTypeToCssClass(Model.ReferenceId))"></span>
确定您的异常来自 Razor 视图而不是控制器?
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
如果结果集为空,.First() 将抛出异常。尝试使用 .FirstOrDefault() 代替