Ronald McDonald
Ronald McDonald

Reputation: 2911

Pass values from Hidden field to ActionLink then to Controller

I'm trying to dynamically set the StudentIds by letting the user select check boxes for the reports they want. When they click the ActionLink, I use jQuery to set the values of the ids in a hidden field. I need the Action Link to read the ids from hidden field.

The values are getting set in the hidden field, but they are not being passed to the controller from the actionLink.

I need to pass the ReportIds to the controller.

        @Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", new { studentIdList = Model.ReportIds }, new { @class = "button print_selected print_selectedMedEd disabled" })



        @Html.HiddenFor(model => model.ReportIds)

javascript

$('.print_selectedMedEd').bind('click', function () {
    var ids = getPrintIds();
    if (ids.length == 0) {
        return false;
    }

    $('#ReportIds').val(ids.toString());


    return true;
});

Upvotes: 1

Views: 11634

Answers (3)

saktiprasad swain
saktiprasad swain

Reputation: 340

             @Html.HiddenFor(modelItem => item.OrderId)
            <td>

                <input type="button" value="Pickup" onclick="location.href='@Url.Action("Edit", "Assignment", new { ID = item.OrderId })'" />

Upvotes: 0

Andres Castro
Andres Castro

Reputation: 1858

That is because @Html.ActionLinkdoesn't use the hidden fields to make the request. Once the action link renders it becomes <a href="/Student/GetMedEdVerificationReport/reportIds..."> PrintMed </a> so you would need to modify the html on the anchor tag.

You should be using Html.Beginform instead in order to pass along the model.

@using (Html.BeginForm("GetMedEdVerificationReport", "Student", FormMethod.Post, null))
    {
        @Html.HiddenFor(model => model.ReportIds)
        <input type="submit" value="PrintMed" />
    }

Upvotes: 2

Felipe Oriani
Felipe Oriani

Reputation: 38608

When the asp.net mvc render your ActionLink it will generate a link with a parameter that you have on the model. Event you change the value of the model, it will not change the value on the output generated by ActionLink.

Given this, you have to se again the value, try to generate an ACtionLink without the argument:

@Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", null, new { @class = "button print_selected print_selectedMedEd disabled" })

On the javascript side, you could try using the on method to bind a event handler and call the preventDefault method from the event argument, for sample:

$('.print_selectedMedEd').on('click', function (e) {
    e.preventDefault();
    var ids = getPrintIds();
    if (ids.length > 0) {
       $('#@Html.IdFor(model => model.ReportIds)').val(ids.toString());

       // make an url to redirect
       var url = $(this).prop("href") + "?studentIdList=" + ids.toString();
       // redirect using the generated url
       window.location.href = url;
    }
});

Remember to use the Html.IdForm() to make sure you have the right id for a specific property.

Upvotes: 5

Related Questions