Reputation: 3601
I have a fairly simple scenario
I have an action method in my controller with syntax like that
[HttpPost]
public ActionResult Consignment(string txtConsNo, string isReferenceTracking)
{}
Not in a page I need to create an hyperlink in application which needs to access this action method. I am using HTML.ActionLink method to create hyperlink like this
@Html.ActionLink((string)item.RDC_CONSNO, "Consignment", "Tracking", new { txtConsNo = (string)item.RDC_CONSNO, rdbConsNo = "" }, null)
But it creates the link like this
http://localhost:3412/Tracking/Consignment?txtConsNo=100245506
How should I go around this?
Thanks
Upvotes: 4
Views: 12467
Reputation: 7135
I'm guessing from your use of item
instead of a model that you're rendering the links in a loop? In any case, I'd suggest adding a form and having the link post it; the link(s) would be like this:
@Html.ActionLink(
(string)item.RDC_CONSNO,
"Consignment",
"Tracking",
new { @class = "consignmentLink" });
...then after the loop (if there is one) you put in a form and some wire-up JavaScript, like this:
@using (Html.BeginForm("Consignment", "Tracking"))
{
@:<input type="hidden" id="txtConsNo" name="txtConsNo" />
}
$("a.consignmentLink").click(function(e) {
e.preventDefault();
var $consignmentNumberInput = $("#txtConsNo");
$consignmentNumberInput.val($(this).text());
$consignmentNumberInput[0].form.submit();
});
To populate your action's isReferenceTracking
parameter you could add another hidden field and have that value as a data- attribute on each link.
Upvotes: 2
Reputation: 2890
Also you can use a button:
for example in asp core syntax:
//some other tags
<form method="post">
<input asp-for="YourModelPropertyOrYourMethodInputName"
value="@TheValue" type="hidden" />
<button type="submit" class="link-button" formaction="/TheDestinationController/TheDestinationActionMethod">
@(TextValue)
</button>
</form>
Upvotes: 1
Reputation: 7126
I guess you have two options....
[HttpPost]
attribute (preferred in my opinion)Here is how to do the jquery approach (if necessary)
The Html:
<a href="#" class="postToConsignment"
data-consno="@item.RDC_CONSNO">@item.RDC_CONSNO.ToString()</a>
The javascript (which needs to be in your view):
$(function(){
$('.postToConsignment').on('click', function(e){
// Stop links normal behaviour
e.preventDefault();
//Get the url to post to
var url = '@Url.Action("Consignment", "Controller")';
//Get consNo
var consNo = $(this.data('consno');
$.post(url, new { txtConsNo: consNo}, function(data) {
//Deal with the result (i.e. the data param)
});
});
});
Upvotes: 2