Reputation: 51
In my controller i have IActionResult, which takes 3 strings
public IActionResult DeviceDetails(string idDetails, string begindate, string enddate)
{....}
I like to pas this strings from web page, where user can chose begin and end date from datepicker, so i have:
<input id="begindate" type="date" class="btn btn-default"/>
<input id="enddate" type="date" class="btn btn-default" />
<button type="submit" value="show data" class="btn btn-default">@Html.ActionLink("Show data", "DeviceDetails", new { idDetails = ViewBag.DeviceName, begindate ="begindate", enddate = "enddate" }) </button>
How can i pass values from id="begindate"
and id="enddate"
to Html.ActionLink
(idDetails works fine)?
Or, how can i pass this two string to controller in different way?
Upvotes: 1
Views: 102
Reputation: 12022
Try this:
ActionLink (default):
@Html.ActionLink("YourAction", "YourController", new { id = item.ID })
ActionLink (using Button):
<button onclick="location.href='@Url.Action("YourAction", "YourController",
new { Model.ProductID })';return false;">Details</button>
or
<input type="button" title="Details" value="Details" onclick="location.href=
'@Url.Action("YourAction", "YourController", new { id = item.ID })'" />
Upvotes: 1
Reputation: 24957
You can generate ActionLink
using temporary placeholder values inside routeValues
and put an ID to anchor element:
<button type="submit" value="show data" class="btn btn-default">@Html.ActionLink("Show data", "DeviceDetails", new { idDetails = ViewBag.DeviceName, begindate = "xxxx", enddate = "yyyy" }, new { id = "showdata" })</button>
Then, use plain JS/jQuery to handle click
client-side event which replaces temporary placeholder values into real values from date input (below is using jQuery):
$('#showdata').click(function(e) {
var beginDate = $('#begindate').val();
var endDate = $('#enddate').val();
var tempValue = $(this).prop('href');
var realValue = tempValue.replace("xxxx", beginDate)
.replace("yyyy", endDate);
location.href = realValue; // redirect
return false; // cancel default redirect
});
As a side note, better to use a strongly-typed viewmodel and pass input values to controller action method using viewmodel properties rather than building large amount of query strings.
Live example: DotNET Fiddle
Upvotes: 1