How to perform enable/disbale or hide/unhide in asp.net mvc 2
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.
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.
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
<% } %>
精彩评论