在我的控制器中,我有类似的东西:
[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是否设置为特定字符串的逻辑,并根据模型中的值更改视图。
但是,我注意到上述工作有时,我发现在切换选项卡时我得到一个null异常。我想知道为什么我的模型是空的,如果我从控制器中获取逻辑是否有意义?
使用我在控制器中设置的内容,如何检查模型在视图中设置为特定值的时间?
编辑:
切换标签时调用的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');
}
});
}
我如何在控制器中返回部分视图:
[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");
}
答案 0 :(得分:1)
您为Razor / CSHTML显示的逻辑似乎很好。 它非常专业地展示/展示,因为它只是选择一种颜色,所以IMO在视图中是合适的。我称之为商业逻辑。
尽管可以使用类似ReferenceTypeToRgbColor
之类的辅助函数使其更干,并在样式中使用它,甚至更好地发出一个类并将您的样式放在CSS中。我也可能建议使用枚举或常量而不是字符串&#34; Proximal&#34;,但这是另一个故事。
@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>
答案 1 :(得分:0)
确定您的例外来自Razor视图,而不是来自控制器吗?
case_info info = db.info.Where(r => r.Id == Id && r.LocationId == (int)LocationEnum.Proximal).Select(r => r).First();
如果结果集为空,.First()将抛出异常。尝试使用 .FirstOrDefault()代替