RollerCosta
RollerCosta

Reputation: 5196

How to post parameter value to some actionlink

In my view i have 10 link every link associated with some unique value. Now i want that associated value at my controller action and from that action i want to redirect the flow to some other action based on that value.
But the condition is i dont want to display it on url.
How can i acheive this?


View

    <ul>@foreach (var item in Model)
    {<li>
  @Ajax.ActionLink(item.pk_name, "Index","Candidate", new { para1= item.para1 }
             , new AjaxOptions { HttpMethod = "POST" })</li>
    }</ul>



Action

[HttPost]
public ActionResult(int para1)
{
return RedirectToAction(para1,"anotherController");
}


I am getting value at para1 with ajax post(that is what i primarily needed) but here also want to redirect my application flow base on para1 value which is action name.
Confision : here i am not sure is this is the right way to do this thing. So i am asking you guys should i go for route map of working with ajax post will solve my objective.

Upvotes: 0

Views: 410

Answers (2)

Cosmin P&#226;rvulescu
Cosmin P&#226;rvulescu

Reputation: 324

If you only need to redirect the user based on what he clicks on without showing him the link, I believe the best way to achieve this is by client-side coding.

In my opinion there is no need to take any request through the server in order to change the page for such a low-complexity redirect.

View

// HTML
// Might be broken, been awhile since I worked with MVC
// Can't really remember if that's how you put variables in HTML
<ul id="button-list">
    @foreach(var item in Model)
    {
        <li class="buttonish" data-para1="@item.para1">@item.pk_name</li>
    }
</ul>

// JS
// I wouldn't do any server related work
$('#button-list li.buttonish').click(function(){
    // Your controller and action just seem to redirect to another controller and send in the parameter
    window.location.href = "/controller/method/" + $(this).data('para1');
});

Upvotes: 1

Dharmik Bhandari
Dharmik Bhandari

Reputation: 1702

I think you should make one jQuery function that is call when clicked and pass unique parameter.In that function you can use AJAX and post it on appropriate controller method.

Example:

<input type="button" id="@item.pk_name" onclick="getbuttonvalue(@item.para1);"  />

In script

 <script type="text/javascript">
 $(document).ready(function () {
function getbuttonvalue(para1) {
            $.ajax({
                cache: false,
                type: "POST",
                dataType: 'json',

                url: "/controller/method/" + para1,

                success: function (data) {
                }

            });

}

 });

</script>

Upvotes: 1

Related Questions