如何在 asp.net mvc 2 中执行启用/禁用或隐藏/取消隐藏
我通过控制器将一些视图数据和视图包传递给视图,下面是代码片段:
IProductRepository prodResp = new ProductRepository();
Product getGarages = prodResp.GetDetailsForGarages((int)Session["EventID"]);
Product getHelmets = prodResp.GetDetailsForHelmet((int)Session["EventID"]);
if (getGarages == null)
{
ViewBag.Garages = null;
}
ViewBag.Garages = getGarages;
int totalGarages = getGarages.QtyAvailable;
var garages = Enumerable.Range(1, totalGarages).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewBag.GaragesDropDown = new SelectList(garages.ToList(), "Value", "Text");
if (getHelmets == null)
{
ViewBag.helmets = null;
}
ViewBag.helmets = getHelmets;
return View(booking);
}
View
<% if (Convert.ToBoolean(ViewBag.boolSecondDriver))
{%>
<lable>Second Driver Availablity For this Event</lable><br />
<lable>Secondriver:</lable> <%: Html.TextBox("SecondDriver") %>
<br />
<lable>SecondriverPrice:</lable> <%: ViewBag.trackday.SecondDriverPrice %>
<br /><br />
<lable>Number of Helmets Available For this Event</lable><br /><br />
<lable>No of Helmets:</lable><%: ViewBag.helmets.QtyAvailable%><br />
<lable>Price per unit:</lable><%: ViewBag.helmets.UnitCost%> <br /><br />
<lable>Number of Garages Available For this Event</lable><br /><br />
<lable>No of Garages:</lable> <%: ViewBag.Garages.QtyAvailable%><br />
<lable>price per unit:</lable><%: ViewBag.Garages.UnitCost%>
<%}
else{ %>
<lable>Second Driver Availablity For this Event</lable><br />
<lable>Free</lable>
<br /><br />
<lable>Number of Helmets Available For this Event</lable><br /><br />
<lable>No of Helmets:</lable><%: ViewBag.helmets.QtyAvailable%><br />
<lable>Price per unit:</lable><%: ViewBag.helmets.UnitCost%> <br /><br />
<lable>Number of Garages Available For this Event</lable><br /><br />
<lable>No of Garages:</lable> <%: ViewBag.Garages.QtyAvailable%><br />
<lable>price per unit:</lable><%: ViewBag.Garages.UnitCost%>
<%} %>
我遇到的问题是,如果 viewbag.value 的值为空,则无法隐藏或取消隐藏 Viewbag。值应该隐藏在视图中,因此我收到错误:无法对空引用执行运行时绑定。任何建议或替代方案将不胜感激。
I am passing some viewdata and view bag to the view through controller , below is the snippet of the code:
IProductRepository prodResp = new ProductRepository();
Product getGarages = prodResp.GetDetailsForGarages((int)Session["EventID"]);
Product getHelmets = prodResp.GetDetailsForHelmet((int)Session["EventID"]);
if (getGarages == null)
{
ViewBag.Garages = null;
}
ViewBag.Garages = getGarages;
int totalGarages = getGarages.QtyAvailable;
var garages = Enumerable.Range(1, totalGarages).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewBag.GaragesDropDown = new SelectList(garages.ToList(), "Value", "Text");
if (getHelmets == null)
{
ViewBag.helmets = null;
}
ViewBag.helmets = getHelmets;
return View(booking);
}
View
<% if (Convert.ToBoolean(ViewBag.boolSecondDriver))
{%>
<lable>Second Driver Availablity For this Event</lable><br />
<lable>Secondriver:</lable> <%: Html.TextBox("SecondDriver") %>
<br />
<lable>SecondriverPrice:</lable> <%: ViewBag.trackday.SecondDriverPrice %>
<br /><br />
<lable>Number of Helmets Available For this Event</lable><br /><br />
<lable>No of Helmets:</lable><%: ViewBag.helmets.QtyAvailable%><br />
<lable>Price per unit:</lable><%: ViewBag.helmets.UnitCost%> <br /><br />
<lable>Number of Garages Available For this Event</lable><br /><br />
<lable>No of Garages:</lable> <%: ViewBag.Garages.QtyAvailable%><br />
<lable>price per unit:</lable><%: ViewBag.Garages.UnitCost%>
<%}
else{ %>
<lable>Second Driver Availablity For this Event</lable><br />
<lable>Free</lable>
<br /><br />
<lable>Number of Helmets Available For this Event</lable><br /><br />
<lable>No of Helmets:</lable><%: ViewBag.helmets.QtyAvailable%><br />
<lable>Price per unit:</lable><%: ViewBag.helmets.UnitCost%> <br /><br />
<lable>Number of Garages Available For this Event</lable><br /><br />
<lable>No of Garages:</lable> <%: ViewBag.Garages.QtyAvailable%><br />
<lable>price per unit:</lable><%: ViewBag.Garages.UnitCost%>
<%} %>
The issue I am having is I cant hide or unhide the Viewbag if the value of viewbag.value is null then the respected viewbag.values should be hidden in the view , therefore I am getting errors: Cannot perform runtime binding on a null reference. Any suggestions or alternatives will be highly appreciated thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用ViewModel?
ViewBag 在处理不需要“模型”的单个字段等时很好,但是那里有很多代码,我看到像 Helmet 和 Garage 这样的名词 -所以你应该使用 ViewModel。
然后你可以使用:
如果
SecondDriver
为 null,则不会呈现任何内容。您可以为
SecondDriver
的任何类型创建一个显示模板,并将标记移动到那里,这意味着您可以在任何视图中重复使用它。不了解你,但是当我进行 MVC 开发时,我的第一目标是保持我的视图干净并且没有代码汤 - 这就是你目前所拥有的。
Have you considered using a ViewModel?
ViewBag is fine when dealing with single fields etc that don't warrant a "model", but you have a lot of code there, i see nouns like Helmet and Garage - so you should be using a ViewModel.
Then you can use:
And if
SecondDriver
is null nothing will be rendered.And you can create a display template for whatever type
SecondDriver
is, and move the markup there, meaning you can re-use it across any Views.Don't know about you, but when i do MVC development, my #1 goal is to keep my View clean and free from code soup - which is what you currently have.
您可以向
ViewBag
添加另一个属性:然后在您的视图中您可以使用此属性来测试 null:
You could add another property to the
ViewBag
:Then in your view you can use this property to test for null: