MVC - Column button pass first grid.row information instead the selected one.

Nov 29, 2013 at 8:12 AM
Hi,
I have costumized a column grid with a button that sould pass row information to the controller.
My problem is that when the button is clicked the row information passed to the controller referes ALWAYS the first row and not the selected row.

This is my code:

@Html.Grid(Model).Columns(columns =>
{
    columns.Add()
   .Encoded(false)
   .Sanitized(false)
   .SetWidth(30)
   .RenderValueAs(o => MyButton(o)); ***See below

   columns.Add(o => o.NDG)
   .Titled("NDG")
   .Filterable(true)
   .SetWidth(5);
}

MyButton is:
@helper MyButton(RiepilogoOperazioniOggiXls R)
{
    Html.BeginForm("SendEmail", "Email", new { NDG = R.NDG, eMail = R.eMail }, FormMethod.Post);
<input type="submit" value="Send mail" name="btnEmail"  />
<label>@Html.DisplayFor(X => R.NDG)</label>
}

Controller code:
    [HttpPost]
    public string SendEmail(String  NDG, string eMail) 
    {
        string ObjM = "Mcv test " + NDG; -> this contains Always NDG of the first row of the grid not the selected.

        return "ok";
    }
Can you help me?
Claudia
Nov 29, 2013 at 12:58 PM
can you try some thing like
<a href="@Url.Action("SendEmail","Email", new {NDG = R.NDG, eMail = R.eMail, @class="send-mail" }) />




<script>

        $(function () {
            $(".send-mail").click(function (event) {
                event.preventDefault();
                var href = $(this).attr('href');
                $.ajax({
                    url: href, type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    success:
                        function (result) {
                            alert(result.length);
                    }
                });
            })
        });
});

</script>
Dec 3, 2013 at 7:49 AM
Thanks bhushanvinay!
It works!