Well, I'm not 100% sure if I fully understand your question, but if I hear you correctly, you donβt know how to put an if statement around the first block of code and / or you have to do it.
Firstly, here's the βhowβ - you just use @if (sorry if this seems obvious - I'm not trying to offend your intelligence):
@if (!string.IsNullOrEmpty(ViewBag.search)) { <div id="clearbuton"><p>@Html.ActionLink("Clear", "")</p> <p> @String.Format("Total of {0} results", ViewBag.CountRecords)</p> </div> }
Now, the reasoning is whether to show the expression βifβ in the view to show or hide the HTML? Oh sure. This is what the point of view in MVC is for. The controller is designed to perform queries and calculate the number of results and manipulate data, but this view should accept the results and actually output them in HTML. Therefore, in this case, we rely on the controller to set the ViewBag.search value, and then, based on this view, can display or hide a specific HTML block. The controller does not and should not know about HTML.
Does this answer your question?
PS- Here's a quick quick reference to Razor syntax if you're interested:
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
source share