proper hyperlinking of asp.net cshtml. currently going to wrong controller - asp.net-mvc

I have a categories controller and a products controller. If the category is a type I reroute to the products page. I keep routing categories to get to subcategories in my controller but at a certain point there are no more subcategories and you have a type rather than a category. i was trying to copy the same logic from my layout which works fine. I've double checked all spelling errors.
<a class="navbar-brand" asp-area="" asp-controller="Categories" asp-action="Index">Eisen Manufacturing Network</a>
(note I added in the istype attribute to my categories model after. I changed the bind properties and the cshtml in order to alter the model)
How do I properly link to a separate controller? because the current method I'm trying below is not working. it takes me to the categories index even though I confirmed that the HTML went to else statement through the debugger.
{
#* #("~/img/"+item.CategoryName+".jpg") *#
<a asp-action="index" asp-route-name="#item.CategoryName">
<text>#item.CategoryName</text>
</a>
}
else
{
<a asp-area="" asp-controller="Products" asp-action="Index" asp-route-type="#item.CategoryName">
<text>#item.CategoryName</text>
</a>
} ```

This is resolved I'm just an idiot and didn't realize that I linked the text and an image below the if statement not shown above. had to use the same logic. works fine. stupid mistakes...

Related

Thymeleaf editing th:field before action

I have a simple pagination that looks like
<ul class="pagination justify-content-end" th:field="*{page}">
<li class="page-item page-item disabled">
<a class="page-link" href="action">1</a>
</li>
</ul>
I would get the following behaviour with Thymeleaf. Clicking on the page and before submitting the action, the value of the th:field associated with the pagination should be changed by setting it to the th:value of the selected page. In other words, clicking on the page number should change the value of "*{page}" and then call the method. Is there any way to do that, for example, combining th:onclick with th:action?
Thymeleaf is a rendering engine, as such it can do nothing once the page is served to the end user. At best u can fill variables into javascript when rendering to achieve the desired result.
As for your described functionality the given code is far to limited or faulty to give a suggestion for a javascript solution, for one th:field on a non-input element doesn't do much. Also the shown href is just a simple href that doesnt interact in any way with the encapsulating th:field.

asp net core activate a controller and action with href? [duplicate]

I'm currently trying to make a website in ASP.NET Core MVC. In my layout page, I'm making a navigation bar to access all of the actions that can be reached through my controllers. I am however unable to create useful links.
<ul>
<li>Home</li>
<li>Index</li>
</ul>
My problem with this is that I still need the controller before the links and if I put the controller in front of the action like this
<ul>
<li>Home</li>
<li>What We've Done</li>
</ul>
When I click on one link and then the other, the link will end up being "myurl/home/home/page".
How can I create the link to only link to the exact page I want to?
You should use the anchor tag helper to build the markup for the link
<a asp-action="index" asp-controller="home">Home</a>
This will generate the correct relative path to the index action as the href property value of the anchor tag.

Use Bootstrap Navbar choice in controller?

All, I'm trying to use the Bootstrap Navbar user choice to control the filtering of posts shown to the user.
The model includes an 'expired' field which is a date-time type.
Three choices are: All (no filtering), Open (show only open issues) and Closed (show closed).
Is there a way to do this without defining three different index.html.erb variants (DRY problem). The filter should show only closed issues if #post.expired < Time.now .. etc.
Stated alternately - can controller 'know' what the user chose, although Navbar, as i am using it, is simply a fancy navigation toolbar?
Thanks for any advice.
Typically this is done by including a parameter in the request, and looking for that parameter in the controller. The bootstrap navbar uses regular anchor links so you should be able to add parameters easily to them (modified example from the doc):
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="/some_url">List</a>
<ul class="nav">
<li>All</li>
<li>Open</li>
<li>Closed</li>
</ul>
</div>
</div>
You can read the filter parameter in the controller by accessing the value of params[:filter].

The Best Way to Highlight or Change Current Menu Item CSS Class in MVC

I have read a few articles as I was searching for a solution. They all seemed to favor a hard-coded or HTML Helper alternative; however, I wanted something simple and database driven. This is my best solution (submitted as an answer, by me).
Here are some other articles' solutions:
An easy way to set the active tab using controllers and a usercontrol in ASP.NET MVC?
asp.net mvc and css: Having menu tab stay highlighted on selection
ASP.NET MVC: Tabs ASCX - Adjust CSS class based on current page?
Just pass a TempData down from one of your controllers like this:
TempData("CurrentPage") = "Nutrition"
Then, in your View add a conditional like this:
<ul>
#For Each item In Model
Dim currentItem = item
If (Not String.IsNullOrEmpty(TempData("CurrentPage")) And TempData("CurrentPage") = currentItem.NAV_Element) Then
#<li><a class="current" href="#Html.DisplayFor(Function(modelItem) currentItem.NAV_Destination)">#Html.DisplayFor(Function(modelItem) currentItem.NAV_Element)</a></li>
Else
#<li>#Html.DisplayFor(Function(modelItem) currentItem.NAV_Element)</li>
End If
Next
</ul>
I have accomplished this in the past by using the route values to generate a href and then setting the parent tab to active based on that.
This is a bootstrap flavoured version:
<ul class="nav nav-tabs">
<li>Some page</li>
<li>Some other Page</li>
</ul>
<script type="text/javascript">
$(function(){
$('a[href="#Url.Action(ViewContext.RouteData.Values)"]').parents("li").addClass("active");
};
</script>
Si
I know I am too late to answer this but if you are using ASP MVC and looking for highlighting Menu items using controller and action, here is the solution which is working perfectly fine for me -
<li> <i class="fa fa-user-plus"></i> <span>Signup</span> </li>

mvc+jqm actionlink html text issue

I'm just starting out with MVC, and I'm trying to build a sample, proof of concept page for mvc interaction with jquery-mobile. I've searched for an answer on this, but I don't think I know enough yet to formulate proper queries.
On my view, I am trying to generate output that functions like this (sampled from the jqm demo site) :
<ul data-role="listview" data-inset="true">
<li><a href="index.html">
<h3>Stephen Weber</h3>
<p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p>
<p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p>
<p class="ui-li-aside"><strong>6:24</strong>PM</p>
</a></li>
<li><a href="index.html">
<h3>jQuery Team</h3>
<p><strong>Boston Conference Planning</strong></p>
<p>In preparation for the upcoming conference in Boston, we need to start gathering a list of sponsors and speakers.</p>
<p class="ui-li-aside"><strong>9:18</strong>AM</p>
</a></li>
</ul>
So, I'm working it like this :
#foreach (var item in Model) {
<li>
#Html.ActionLink( LargeExpandedMultiContentText, "Edit", new { id = item.SysID })
</li>
}
My question, which I don't know a simple way of asking, is, how, in MVC terms, do I fill the variable, so that the href generated by ActionLink() matches the style I've sampled from the jqm website?
I've tried this, and while it almost works, I'm sure its not the 'right' way, especially when I get to a spot of pulling "Steven Weber" and other strings out the the item/Model...plus it encodes the text, but I'm sure there's a way to deal with that easy enough.
#foreach (var item in Model) {
<li>
#{ String txt = "<h3>Stephen Weber</h3><p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p><p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p> <p class=\"ui-li-aside\"><strong>6:24</strong>PM</p>";
}
#Html.ActionLink( txt, "Edit", new { id = item.SysID })
</li>
}
Thanks, I appreciate your help
Brian
I'm not sure it's possible to use Html.ActionLink to build such a complex action link. That said, the Html Helpers are just supposed to be helper functions to make your life easier by giving you shortcuts to render HTML in ways commonly required. You don't need to use them. In this case, I think instead you should use a normal html tag, and Url.Action to create the correct URL.
If you wanted to include this information within an enumerable model I'd probably do something like:
#foreach (var item in Model) {
<li><a href="#Url.Action(item.Action)">
<h3>#item.Name</h3>
<p><strong>#item.Title</strong></p>
<p>#item.Message</p>
<p class="ui-li-aside"><strong>#item.Time</strong>#item.TimePeriod</p>
</a></li>
}
Note: the function you were looking for to display contents without encoding them is #Html.Raw
In answer to my other 'half question' I found this resource...
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
I believe the "razor delegate" item there at the bottom would fit the need I had.
#{
Func<dynamic, object> b =
#<strong>#item</strong>;
}
#b("Bold this")

Resources