Search code examples
asp.net-mvcrotativa

print page using Rotativa in asp.net mvc


I am using Rotativa to print page in my application in asp.net mvc .

it is work well but my problems is i want to print the filter data .

For example, I have Position tables and Teachers , so when click to Position A the all teacher of the Position A show and i put link for print inside each Position.

this is my controller :

  public ActionResult IndexById(int? id)
    {
        var teachersD = db.TeacherDetails.AsQueryable().Include(s => s.Position);
        if (id != null) teachersD = teachersD.Where(c => c.PositionId == id);
        ViewBag.result = teachersD;
        return View(teachersD.ToList());
    }


    public ActionResult Print(int id)
    {
        var report = new ActionAsPdf("IndexById", new { PositionId = id });
        return report;
    }

and this View :

 <td>
        @Html.ActionLink("Teacher", "Index", "TeacherDetails", new { id = item.Id }, null)
    </td>

    <td>
    <td>
        @Html.ActionLink("Print ", "Print", new { id = item.Id })

    </td>        

Solution

  • You're passing a value called PositionId:

    new ActionAsPdf("IndexById", new { PositionId = id })
    

    But the action is expecting a value called id:

    public ActionResult IndexById(int? id)
    

    The framework's model/value binding is based on the names, so they need to match. Since id is missing in the request, the value ends up being null. (If it was an int instead of an int? you would get an error about a missing required value.)

    Simply pass the value as id:

    new ActionAsPdf("IndexById", new { id = id })