Creating a split-button-dropdown in asp.net MVC with Twitter Bootstrap - asp.net

I am trying to create a split button dropdown using Bootstrap and ASP.NET MVC 5, but I cannot seem to render the correct markup. This is what I have so far...
<div class="btn-group">
#Html.ActionLink("Details", "Details", new { id = item.ClientTypeID }, new { #class = "btn btn-sm btn-primary" })
<a class="btn btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>#Html.ActionLink("Edit", "Edit", new { id = item.ClientTypeID })</li>
<li>#Html.ActionLink("Delete", "Delete", new { id = item.ClientTypeID })</li>
</ul>
</div>
I have no doubt that this is my own fault, but can anybody please point out what I should actually have in my template. Instead of the required drop down, I am seeing the initial "Details" button, and I am seeing the drop down caret, but clicking on the caret does not show the additional items.
In case it makes a difference, I am using the default templates from VS2013 along with the Cosmo theme from Bootswatch. This is also being rendered in a table cell.
Thanks.

Make sure you have registered jQuery before bootstrap.min.js.
Debug the error using Developer Tools available in all modern browsers.
Chrome use Ctrl+J, IE use F12, FireFox use Shift+F2
Happy Coding..

Related

Redirecting by buttonclick in asp.net

I have a very simple issue in my asp.net core mvc project.
I have a button in one of my views, and when this button is clicked, I want to redirect to a specific controller, and a action in this controller.
Currently it looks like this:
<button class="dropbtn" class="glyphicon glyphicon-user"> something</button>
I've been looking around for methods of doing this, and one of the solutions I found, was the #Html.ActionLink("pik", "Index", "Home").
So I tried:
<button onclick="#Html.ActionLink("pik", "Index", "Home")" class="dropbtn" class="glyphicon glyphicon-user"> Log in </button>
However this is just displayed as raw html. Does'nt work.
Then, here I found this:
<button nclick="location.href='<%: Url.Action("Action", "Controller") %>'" class="dropbtn" class="glyphicon glyphicon-user"> Log in
However, my IDE just gives a bunch of unexpected token in this line. I don't undestand it either.
I then found this:
#using (Html.BeginForm("Index", "Home", FormMethod.Post)){
<button type="submit" class="btn btn-default" class="dropbtn" class="glyphicon glyphicon-user">Submit</button>
}
This works in terms of the button, but for some reason, when I wrap the button in a form, my styling gets messed up.
I would rather not use javascript for redirecting.
So how can I redirect to a controller action with a button?
#Html.ActionLink() creates an html link with an <a> tag. If you are using Bootstrap (highly recommended) you can use this.
Click Me
or if you want to use helpers, like this
#Html.ActionLink("pik", "Index", "Home", new { #class = "glyphicon glyphicon-user" })
This is the cleanest way I know of to create button with a link.
You can also check this out.

Thymeleaf setting model field with th:field on button dropdown not working

I am trying to set the field on my model object using thymeleaf in a form.
setting th:field on the button is giving back all null properties. They are all formatted the same but this is the look:
<div class="dropdown">
<button th:field="*{product}" class="btn grey-dropdown dropdown-toggle" type="button"
data-toggle="dropdown">(Select)<span class="caret"></span>
</button>
<ul class="dropdown-menu filter-dropdown-items">
<li th:each="product : ${products}"><a><span
th:text="${product}" th:remove="tag"></span></a></li>
</ul>
</div>
Am I doing something wrong? The post comes back with null fields.
As far as I can tell, th:field only supports the following tags:
input
select
textarea
If you want to bind a button, I think you're going to have to do it manually. (th:field sets the name, id, and value attributes so it shouldn't be difficult to set them to the correct values.)

how to add glyphicon to bootstrap dropdown menu

Im trying to add a glyphicon to the left of a link in a dropdown menu, but cant seem to figure it out. This is what my code looks like:
<div class="col-md-4">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">options
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><i class="glyphicon glyphicon-flag"></i><%=link_to "report link", report_path(report: {reportable_id: post.id, reportable_type: "Post"}), id: "report_#{post.class}_#{post.id}", class: "report_link", remote: true, method: :post %></li>
Can someone please explain to me how to do this?
Thanks!
Simply put the icon inside the link - there's no visual downside to doing so, and even potentially improves UX (you can click on the icon too).
<li><i class="glyphicon glyphicon-flag"></i> link</li>
See this bootply
So your new ruby link would look like
<%=link_to "<i class='glyphicon glyphicon-flag'></i> report link".html_safe, report_path(report: {reportable_id: post.id, reportable_type: "Post"}), id: "report_#{post.class}_#{post.id}", class: "report_link", remote: true, method: :post %>
(Apologies if this is wrong, I don't actually know Ruby. I assume this was a css positioning error, but if the Ruby is wrong too let me know and I shall delete this answer)
Try using a span outside of your link_to. You would remove the display text ("report link") after the link_to and add it back after the Glyphicon span class ends.
<li>
<%=link_to report_path(report: {reportable_id: post.id, reportable_type: "Post"}), id: "report_#{post.class}_#{post.id}", class: "report_link", remote: true, method: :post do %>
<span class="glyphicon glyphicon-flag"></span> Report Link
<% end %>
</li>

Bootstrap dropdown: change dropdown icon when it's active?

I have the following setup:
<div data-ng-repeat="item in navigationBar.navObject.items" class="btn-group">
<button data-ng-class="{current: $last}" class="btn btn-default btn-xs" type="button" data-ng-click="navigationBar.goToState(item)"> {{item.name}}</button>
<button data-ng-hide="item.isTerminal" data-toggle="dropdown" class="btn btn-default btn-xs dropdown-toggle" type="button">
<span class="fa fa-fw fa-caret-right"></span>
</button>
<ul class="dropdown-menu">
<li data-ng-repeat="subItem in item.subItems"><a data-ng-click="subItem.item.goToState()">{{subItem.name}}</a></li>
</ul>
</div>
And I would like the icon in the dropdown to change from fa-caret-right to fa-caret-down when the dropdown list is visible. Is there any way to do this strictly with CSS?
You won't be able to change the attributes of HTML elements using CSS, you'll probably want to use jQuery instead.
Bootstrap has some nifty JavaScript going on, where you can execute your own scripts when boostrap elements inside your website is used.
Initialize dropdown using this inside jQuery.
$('.dropdown-toggle').dropdown();
When it's called you can hook different actions to it using the handles specified on the bootstrap docs.
on('show.bs.dropdown', function(){}); is just a fancy word for saying "on dropdown"
$('.dropdown-toggle').on('show.bs.dropdown', function () {
// All actions to fire after the dropdown is shown go here.
$('.fa .fa-fw').addClass('fa-caret-right');
$('.fa .fa-fw').removeClass('fa-caret-down');
})
I know this is an old post, but I was looking for this solution and have one that works for me. You can target and change the pseudo element inside that span. When you click the button, doesn't the button add an open class? So, then the css would look like:
btn.open .fa-caret-right:before { content: "\e114"; }
Use the .open added to the button class.
The \e114 being the character of the down arrow.

Image on ActionLink with Twitter Bootstrap

I have a button at the moment, rendered in my MVC application as:
#Html.ActionLink("Delete", "DeleteTransfer", "Transaction", new { transferId = (Model.Id) }, new { #class = "btn btn-danger", #onclick = "return confirm('Are you sure you want to delete this Transfer?')" })
I want to use the Twitter Bootstrap classes to add an icon. However, for that the code would look like this:
<button class="btn btn-danger"><i class="icon-white icon-trash"></i> Delete</button>
How can I apply this in my ActionLink? It has an Image (i) embedded in the button.
Try:
<a class="btn btn-danger"
href="#Url.Action("DeleteTransfer", "Transaction", new { transferId = (Model.Id) })"
onclick = "return confirm('Are you sure you want to delete this Transfer?');"
>
<i class="icon-white icon-trash">Delete</i>
</a>
According your routes, it's possible you need:
#Url.Action("DeleteTransfer", "Transaction", new { transferId = (Model.Id) }, null)
According to the bootstrap documentation (http://getbootstrap.com/components/#glyphicons-examples) glyphicons might be the way to go. Here is an example of putting an icon on a button:
<button type="button" class="btn btn-default btn-lg">
<span class="glyphicon glyphicon-star"></span> Star
</button>
Since ActionLink() is an htmlHelper extension that returns an "a" element, i.e., <a>...</a>, I believe the extension needs to be overridden.
I tried the following, but it resulted in the glyphicon being above the "a" element:
<span class="glyphicon glyphicon-star"></span>#Html.ActionLink("Delete", "DeleteTransfer")
I also tried putting the ActionLink() inside the <span></span> with the same results. I tried using css with text-wrap: none; and white-space: nowrap; but that didn't work either.
So I believe the answer is the extension needs to be overridden to accept another parameter for the glyphicon. Unless, someone can show a trick to make it stop wrapping.

Resources