Grails controller populated menu with separate main controller view - grails

I am trying to create a membership directory. I have the directory filled out and ready to go. I am using the main.gsp layout and have created the templates for it.
The side navigation bar needs to be populated by the committees and boards that the members belong too. I have created that through a separate domain and controller. The view that it creates makes links that pass search parameters by the committee.id to the member page to sort the list of members.
I am trying to populate that side navigation bar with the committee control throughout the layout of the site but when I got to test out the site that template section does not populate. It just populates when the committee controller page is selected that I created to test my output. I believe that I am missing a call to the controller or some reference that makes this view active throughout the site and not just on the controller page.
I am using Grails v. 2.3.3
Here is the code for the side navigation bar in main.gsp.
<div id="leftNav" >
<g:render template="/common/left_nav" model="[hospital:Hospital]" />
</div>
Here is where the navigation template is called in "_left_nav.gsp".
<div>
<g:render template="/hospital/committee" model="[hospital:Hospital]" />
</div>
Here is where the navigation template is called again in "_committee.gsp".
<%# page import="trustees.Hospital" %>
<div id="hospcomm" class="content scaffold-list" role="navigation">
<table>
<g:each in="${hospitalInstanceList}" status="i" var="hospitalInstance">
<tr>
<td>
<g:link controller="hospital" action="show" id="${hospitalInstance.id}">${fieldValue(bean: hospitalInstance, field: "hospitalName")}</g:link>
<%-- <g:link action="show" id="${hospitalInstance.id}">--%>
<%-- <a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top">--%>
<%-- <img src="/Trustees/static/images/img/navigate.msh_board.gif" border="0">--%>
<%-- </a>--%>
<%-- </g:link> --%>
</td>
</tr>
<tr>
<td>
<ul>
<g:each in="${hospitalInstance.committees}">
<li>
<g:link controller="hospital" action="show" id="${it.id}"> ${it.committeeName} </g:link>
</li>
</g:each>
</ul>
</td>
</tr>
</g:each>
</table>
</div>
The controller for this view is being called dynamically.

Yes. The side navigation bar would only be populated when you access that particular controller's action. You have 2 options, compute this value in every controller's action that would use a view with the side navigation bar or use a filter
If you want this info on every page, I would recommend you trying to make this accessible by using a Filter. You can retrieve hospital everytime a page is loaded.
If you are using a Security plugin, such as Shiro or SpringSecurity as part of your app, you could place this bit there.

Related

Pass one of many objects from view to controller

In an ASP.Net Core web app, I'm passing a List<Parameter> to a view via ViewBag.Parameters. The view also has a SelectParameterViewModel that consists of one property only, an int ParameterId. The objects are then displayed in a table like so (I skipped straight to tbody as the view works fine from a cosmetic perspective):
#model WebApp.Models.ReportViewModels.SelectParameterViewModel
[...]
<tbody>
#foreach (var item in ViewBag.Parameters)
{
<tr>
<td class="col-xs-1">
<form asp-controller="Report" asp-action="Launch">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="#Model.ParameterId" type="hidden" value="#item.Id" />
<input type="submit" class="btn btn-primary btn-sm" value="Select" />
</form>
</td>
<td class="col-xs-8">#item.Description</td>
<td class="col-xs-3">
<a asp-action="Edit" asp-route-id="#item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="#item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="#item.Id">Delete</a>
</td>
</tr>
}
</tbody>
I would like the user to click on the button of the chosen table-row and submit either that row's Parameter (preferred) or the Parameter's Id, like I tried to do in this case. I would then do the same with the <a> tags on the third column. The point is that I don't want the user to simply type /Report/Launch/id and run the report, as each user would have its own sets of parameters and should not be allowed to use any of the others', and the ViewBag only contains the list of its Parameter. I can change the view model and/or the receiving controller's argument depending on the solution, both would work. I tried a few versions of the above html, but none would work, while the above returns ParameterId with value 0.
I couldn't find a similar scenario in other SO questions. There were some solutions that involved JavaScript and, correct me if I'm wrong, but that would still show the ParameterId value in the webpage source. While that would be sort-of-ok in this specific case, I have other cases where I definitely would not want that to happen.
What would be an elegant solution to achieve this? Thank you!

In an MVC view, what controller and method is called if none is specified in the form tag?

I have a MVC project I inherited. On the views, there's no route (controller and action) specified in the BeginForm tag. The view renders correctly so I assume it is picking a default route.
My question is how does it know what route to use if one isn't specified? What's the best practice here: should you specify a route or let it default?
So the view is Views/Config/WorkCodes.cshtml and the tag is
Html.BeginForm()
It goes to the controller ConfigController.cs and calls action WorkCodes(). If I was doing the project, I would have wrote
Html.BeginForm("WorkCodes", "Config", FormMethod.Post)
How does MVC know which controller and action to use without specifying it?
WorkCodes.cshtml
#{
ViewBag.Title = "Work Codes";
}
#using (Html.BeginForm())
{
<div>
<table style="width: 100%;" class="trHoverHighlight">
<tbody>
<tr>
<td>
<br />
<div>
<button id="buttonCreateNew" type="button">Add New</button>
<button id="buttonReturn" type="button">Return</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
}
ConfigController.cs
public ActionResult WorkCodes()
{
return View(Rep.GetWorkAll(true));
}
Here's where the view is called from in another view:
#foreach (var itm in (List<string>)ViewBag.ListObjects)
{
<li>
Work Codes
</li>
}
The View is generated from performing HTTP GET to the WorkCodes controller action, thus by default the form generated in that view performs an HTTP POST to a controller action with the same name.
Here's the MSDN docs.
BeginForm(HtmlHelper) Writes an opening tag to the response.
The form uses the POST method, and the request is processed by the
action method for the view.

Show Div in another view

I have a navigation cshtml view and a index view. The nav view is on the left hand side, and when I click an item on that view I want to display a partial view inside a div in the Index view.
Here is what I am trying when a user selects a item in the Nav View
$.get('Home/ItemResult/', { id: item.value}, function (data) {
$('#partialPlaceHolder').html(data);
});
}
In my index I have this (I commented out the Html.Action (I was using that, but need to pass a parameter from the nav view))
<table class="tg">
<tr>
<th class="tg-031e">
<div id="partialPlaceHolder" style="display:none;">
</div>
#*#Html.Action("ItemResult", "Home")*#
</th>
</tr>
</table>
my layout refernces the nav view.
<!-- Wrapper-->
<div id="wrapper">
<!-- Navigation -->
#Html.Partial("_Navigation")
<!-- Page wraper -->
<div id="page-wrapper" class="gray-bg">
<!-- Top Navbar -->
#Html.Partial("_TopNavbar")
<!-- Main view -->
#RenderBody()
<!-- Footer -->
#Html.Partial("_Footer")
</div>
<!-- End page wrapper-->
</div>
It is hitting the controller action, but the view is not showing in the placeholder div. Is this possible? If so, What am i doing wrong?
The style of the partialPlaceHolder contains display:none; which means you won't see the result.
Remove the offending code.

template navigation using Grails controllers

I am new to Grails and I am trying to get a template navigation bar to be populated dynamically through a controller. I am really close to solving this but seem to have run into a wall lately.
I currently am getting the navigation to populate only when I click on the link for that controller. Every where else the navigation just populates the bullet points for the links. I am probably not referencing the controller correctly in my template but have not found any good examples yet.
using Grails 2.3.3
the controllers are dynamic.
Here is the code for my navigation template
<body>
<!-- Links to the committees go here -->
<div class="leftMenu">
<!-- template of hospitals and there committees goes here -->
<div>
<g:render template="/hospital/committeeTemp" />
</div>
<tr valign="top">
<td>
<a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top">
<img src="/Trustees/static/images/img/navigate.events.gif" border="0">
</a>
</td>
</tr>
<tr valign="top">
<td>
<a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top">
<img src="/Trustees/static/images/img/navigate.news.gif" border="0">
</a>
</td>
</tr>
<tr valign="top">
<td>
<a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top">
<img src="/Trustees/static/images/img/navigate.help.gif" border="0">
</a>
</td>
</tr>
<%-- </table>--%>
</div>
</body>
I am using a nested loop to populate the navigation bar. That code can be found here Grails navigation links nested loop
With a little more knowledge under my belt. I found out that it was only populating on that one page because that is where it was accessing that particular controller's action. So I ended up putting that mapping into ever action in my controller.

asp.net mvc calling different usercontrol from different view

I Have a View Folder
FrontEnd
JobDetails.ascx (View)
Another View Folder
Job
Apply.ascx (view)
I have a Apply (a href) in jobdetails which have a show and hide div mechanism for apply (Rendering Apply.ascx in JobDetails
<div id="div1" style="visibility:hidden">
<% Html.RenderPartial("../../Views/Jobs/Create"); %>
</div>
my create View in job
<% using (Html.BeginForm("Create", "Jobs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true) %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td></td>
</tr>
<tr>
<td>
<% } %>
the question is that how would i go back in the JobDetails View if some Error occurs in my create form to display the errors there . I am at lost here , hope that the question is clear enough.
Probably the simplest way to handle this is to do the post of the apply via AJAX and simply render the apply form with the errors in place by replacing the existing HTML with that returned when the apply fails. If javascript is turned off, then it will render just the failed application but that seems like a reasonable trade-off to me.

Resources