Passing value from view to controller by clicking an attribute - view

I am trying to pass these values back to my controller for some basic paging and I am not sure why it is not getting them.
Am I using asp-route incorrectly to pass them?
View:
<ul class="pager">
<li class="previous"><a asp-action="Index" asp-route-prev="#Model.First().ID">Previous</a></li>
<li class="next"><a asp-action="Index" asp-route-next="#Model.Last().ID">Next</a></li>
</ul>
Controller:
[HttpPost, ActionName("Index")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> IndexPost(string certification, string category, string resource, int prev, int next)

<a asp-action="Index" asp-route-prev="#Model.First().ID">Previous</a>
Above code renders a tag which makes HttpGet request to action method, if you click on it.
If you want to make HttpPost, you will need to use form tag, and button or input tag with type submit.
<ul class="pager">
<li class="previous">
<form asp-controller="Home" asp-action="Index"
asp-route-prev="#Model.First().ID" method="post" role="form">
<button type="submit">Previous</button>
</form>
</li>
</ul>
Another way is to post the form using JavaScript or jQuery on a click, but it is a bit out of the scope of original question.

Related

How to transfer data from a html page to another html page in spring boot?

I'm building buy function in my website and i want when the user click the buy button, the information of the product (productName and price) will auto fill in the input of the bill form, i tried to use #PathVariable and #RequestParam but it seems doesn't work.
this is product.html
<div th:each="product:${food}">
<form th:action="#{/payment}">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title" th:text="${product.productName}" th:name="productName"></h5>
<p class="card-text" th:text="${product.description}" th:name="description"></p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item" th:text="${product.productType}" th:name="productType"></li>
<li class="list-group-item" th:text="${product.price}" th:name="price"></li>
<button type="submit" class="btn btn-primary">Buy</button>
</ul>
</div>
</form>
</div>
this is the function in Controller
#RequestMapping("/payment")
public String buy(#RequestParam("productName") String name, #RequestParam("price") Float price, Model model) {
model.addAttribute("name", name);
model.addAttribute("price", price);
return "payment";
}
What did i wrong in the syntax or do we have anyway to transfer data from the html pages? Thanks u guys a lot
In your form tag you missed the object to be transmitted to your controller. All the field from your form need to be place in an object which will be catched by your controller thanks to the annotation #ModelAttribute.
It will look like:
<form th:action="#{/payment}" th:object="${product}" >....
and in your controller:
public String buy(#ModelAttribute("product") Product product, Model model) {...}

Implement search from inside layout in asp.net core mvc

I'm creating my first web app using asp.net core mvc, so I'm stuck with implementing global search using textbox inside layout, I can't find where to implement the search after user press Enter, Is it implemented in controller or something else, and how to implement it...
I'm stuck with implementing global search using textbox inside layout, I can't find where to implement the search after user press Enter
To implement the above requirement, you can refer to the following code snippet.
Adding form with input for searching functionality in _Layout.cshtml
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
<li class="nav-item">
<form id="f_search" method="get" asp-action="Search">
<div class="form-group">
<div class="input-group">
<input id="ipt_search" type="search" class="form-control" name="SearchTerm" />
<span class="input-group-btn">
<button class="btn btn-default">
Search
</button>
</span>
</div>
</div>
</form>
</li>
</ul>
</div>
JS code of checking keydown event and user input
<script>
$(function () {
$('#f_search input').keydown(function (e) {
var searchTerm = $("#ipt_search").val();
if (searchTerm == "" && event.keyCode === 13) {
e.preventDefault();
return false;
}
});
})
</script>
Action method of Search
public IActionResult Search(string SearchTerm)
{
//...
//do query based on SearchTerm
//code logic here
//...
return View();
}
Test Result

Why does a link with route set to posts.destroy in practice routes to posts.show

#extends('layouts.app')
#section('content')
<ul>
#foreach ($posts as $post)
<li>
<a href="{{route('posts.show', $post->id)}}">
{{$post->title}}
</a>
 
<a href="{{route('posts.edit', $post->id)}}">
Edit
</a>
 
<a href="{{route('posts.destroy', $post->id)}}">
Delete
</a>
#endforeach
</ul>
#endsection
I am new to laravel and in the process of learning. In the code above i have made a simple Unordered list of posts that is in the database. Next to each post is a edit and delete links. The edit link works just fine and invokes edit method in posts controller. But the delete link doesn't work. I think it runs the show method instead of destroy method in posts controller. Why is this the case?
To be more clear i have already somewhat solved the problem with the code bellow inside the foreach directive
<form action="/posts/{{$post->id}}" method="post">
#csrf
<input type="hidden" name="_method" value=" DELETE">
<input type="submit" name="delete" value="Delete" id="">
</form>
But why do this in the first place. I understand when we use form since a html form doesn't support DELETE method i had to add the hidden input. But why doesn't the hyper link tag work. This is the list of routes:
Links always perform GET method by default.
You can mimic DELETE method behavior using some javascript.

How to pass Renderpartial view in _Layout page

Here i create a partial view when i try to implement in _Layout page its throughing an Error as cant convert void to object
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>#Html.RenderPartial("_AutoCompltetext")</li>
</ul>
</div>
<li>
#{
Html.REnderPartial("_Autocomplte")
}
</li>

Route data to #yield section in Laravel

I'm new in Laravel and need to pass data to navbar in header.
This are my pages
Main:
<div class="container">
<header>
#include('user.includes.header')
</header>
<main>
#yield('content')
</main>
<footer>
#include('user.includes.footer')
</footer>
</div>
Header:
<div id="navigation">
<nav class="navbar navbar-default">
<ul class="nav navbar-nav navbar-left">
<li class="dropdown">
#foreach($categorias as $cat)
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" title="{{$cat->nome}}">
{{$cat->nome}}
<span class="caret"></span>
</a>
#endforeach
</li>
</ul>
</nav>
</div>
IncludesController:
class IncludesController extends Controller
{
public function Header(){
$categorias = Categoria::all();
return compact('categorias');
}
}
}
I want to pass data form Controller to header.
I tried this route:
Route::get('header', 'IncludesController#Header');
but doesn't work and with this route:
Route::get('/', 'IncludesController#Header');
only shows the data and o want to all HTML page.
In IncludesController u need to return :
class IncludesController extends Controller
{
public function Header(){
$categorias = Categoria::all();
return view('main',compact('categorias');
}
}
}
I hope your main.blade.php file is directly under resources/views folder.
This is untested but it might probably help...
You should be returning your content view from your controller, the data to pass to that view is the second parameter.
return view('yourContentView', compact('categorias'));
As long as your content view #extends('main'), any data passed to your content view would also be made available to both your header and footer views.
Another solution would be to setup a view composer for both your header and footer views. This will fire each time those views are loaded and automatically inject data into that view...
You can read documentation on the view composers at https://laravel.com/docs/5.2/views#view-composers

Resources