如何在 asp.net mvc 2 中执行启用/禁用或隐藏/取消隐藏

发布于 2024-11-17 09:44:56 字数 3175 浏览 5 评论 0原文

我通过控制器将一些视图数据和视图包传递给视图,下面是代码片段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-11-24 09:44:56

任何建议或替代方案都将受到高度赞赏。

您是否考虑过使用ViewModel

ViewBag 在处理不需要“模型”的单个字段等时很好,但是那里有很多代码,我看到像 HelmetGarage 这样的名词 -所以你应该使用 ViewModel。

然后你可以使用:

<%: Html.DisplayFor(model => model.SecondDriver) %>

如果 SecondDriver 为 null,则不会呈现任何内容。

您可以为 SecondDriver 的任何类型创建一个显示模板,并将标记移动到那里,这意味着您可以在任何视图中重复使用它。

不了解你,但是当我进行 MVC 开发时,我的第一目标是保持我的视图干净并且没有代码汤 - 这就是你目前所拥有的。

Any suggestions or alternatives will be highly appreciated thanx.

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:

<%: Html.DisplayFor(model => model.SecondDriver) %>

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.

南冥有猫 2024-11-24 09:44:56

您可以向 ViewBag 添加另一个属性:

ViewBag.MyStuffIsNull = (MyStuff == null);

然后在您的视图中您可以使用此属性来测试 null:

<% if (ViewBag.MyStuffIsNull) { %>
    // Do work
<% } %>

You could add another property to the ViewBag:

ViewBag.MyStuffIsNull = (MyStuff == null);

Then in your view you can use this property to test for null:

<% if (ViewBag.MyStuffIsNull) { %>
    // Do work
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文