0

My View is

    @model IEnumerable<Trials.ViewModels.PList>
    @foreach (var item in Model) {
        string target = "";
        if (ViewBag.func == "Screening")
        {
            target = "DivDialogDiv_Scrng";
        }
        else if (ViewBag.func == "Eligible")
        {
            target = "DivDialogDiv_Eligible";
        }

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.UnitNo)

            </td>
                        <td>
                @Html.DisplayFor(modelItem => item.PName)
            </td>
            <td>
               @Html.DisplayFor(modelItem => item.PAge)
               @Html.HiddenFor(x=> item.PAge)
            </td>
            <td>
                @Html.ValueFor(modelItem => item.StartDate, "{0:dd/MM/yyyy}")
            </td>
            <td>
                @Html.ValueFor(modelItem => item.EndDate, "{0:dd/MM/yyyy}")
            </td>
            <td>


     <b>@Ajax.ActionLink("Screen", "getQuestions", "Screening",new AjaxOptions { HttpMethod = "GET",  LoadingElementId="divLoading", UpdateTargetId =@target, InsertionMode = InsertionMode.Replace,OnSuccess="openDialog"})</b>
   @Html.HiddenFor(modelitem=> item.TId)

            @Html.HiddenFor(x=> item.Status_Id)
            @Html.HiddenFor(x=> item.UnitNo)
            @Html.HiddenFor(x=> item.ResponseID)

             </td>
    </tr>
}
 </tbody>
</table>

and in the controller i receive as

public ActionResult getQuestions(T.ViewModels.PList pl)
        {
            List<PatientQuestions> model = rep.getQuest(pl.TId,User.Identity.Name);
            return PartialView("_getQuestions",model);
        }

The problem is that I am getting 0 and null values in the controller . Though I pass as hidden but still in controller I am not getting any values passed from View

Khan
  • 109
  • 2
  • 12

1 Answers1

0

Because @Ajax.ActionLink simply invokes an action, without posting any additional data.

If you need to post data with, you need to use `@using(Ajax.BeginForm)'. See docs here, and sample here.

When you use Ajax.BeginForm within an using(){} block, it renders the <form> and </form> tags, and all the input controls rendered inside the block are included in this form. Then, when the form is submitted, all the data of this input is sent to the controller, as you expect.

A very simple example of this:

<div id="result"></div>

@using (Ajax.BeginForm("getQuestions", "Screening", 
    new AjaxOptions { UpdateTargetId = "result" }))
{
    @Html.HiddenFor(modelitem=> item.TId)
    @Html.HiddenFor(x=> item.Status_Id)
    @Html.HiddenFor(x=> item.UnitNo)
    @Html.HiddenFor(x=> item.ResponseID)
    <input type="submit" value="Screen" />
}

Obviously you must modify this code to do exactly what you need to do (specify all the required ajax options). And take into account that, when you want to post data you should use the "POST" method instead of the "GET" method, and decorate your action to be able to receive a post request ([HttpPost]).

NOTE: You can send all the data in a GET request, in the URL query string, but it's much better to POST it, in the request body.

IMPORTANT: although you must already be using them, don't forget to include the jQuery Unobtrusive AJAX scripts.

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117