manav inder
manav inder

Reputation: 3601

Create hyperlink (anchor tag) in MVC to post data to controller

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

Answers (3)

Steve Wilkes
Steve Wilkes

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

Elnaz
Elnaz

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

NinjaNye
NinjaNye

Reputation: 7126

I guess you have two options....

  1. Remove the [HttpPost] attribute (preferred in my opinion)
  2. Use jquery to post: $.post()

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

Related Questions