TjDillashaw
TjDillashaw

Reputation: 817

getJSON not passing correct value to method in controller

I spend almost whole day to find solution for this problem. I have no idea how to fix this.

This is my view:

<div id="Opis"></div>

<script>
    $(document).ready(function() {
        var idwyc = 2;
        $.getJSON('@Url.Action("Test","Pracownik_biurowy")',{ selectedWycieczka: idwyc } , function (data) {

            $('#Opis').append(data)
        });
    });
</script>

and method in Pracownik_biurowyController:

public ActionResult Test(int selectedWycieczka)
{

    var wycieczkaDetails =
       db.Wycieczka_fakultatywna.Where(w => w.Id_wycieczki == selectedWycieczka).Select(x => new
       {

           x.Opis,
           x.Koszt
       });

    return Json(wycieczkaDetails,JsonRequestBehavior.AllowGet);
}   

I think it should work but I get all the time error.

The parameters dictionary contains a null entry for parameter 'selectedWycieczka' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Test(Int32)' in 'BiuroPrototyp.Controllers.Pracownik_biurowyController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Nazwa parametru: parameters

RouteConfig:

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Upvotes: 0

Views: 165

Answers (2)

TjDillashaw
TjDillashaw

Reputation: 817

Okay I'm still don't know to fix this problem but I found how to work around this. Maybe it is not good practice but it works. Im just taking id value from Request[] attribute

Method:

   public ActionResult GetWycieczkaDetails()
    {
        var id = Request["idWycieczka"];
        int selected = Int32.Parse(id);
        var wycieczkaDetails =
            db.Wycieczka_fakultatywna.Where(w => w.Id_wycieczki == selected).Select(x => new
            {
                x.Opis,
                x.Koszt
            });



        return Json(wycieczkaDetails.ToList(),JsonRequestBehavior.AllowGet);
    }

Script:

    $('#SelectedWycieczka').change(function () { // <-- my dropdown
    var id = $(this).find(":selected").val();


    var target = '@Url.Action("GetWycieczkaDetails", "Home")?idWycieczka=' + id;
    $.getJSON(target,  function (data) {

        if (data.length > 0) {
            for (i = 0; i < data.length; i++) {
                $('#Opis').append(data[i].Opis);
                $('#Koszt').append(data[i].Koszt);
            }

        }
    });


});

Upvotes: 0

venerik
venerik

Reputation: 5904

You are passing a JSON-object which can't be mapped to your routes because in the route you specify {id} but you are providing selectedWycieczka.

Change { selectedWycieczka: idwyc } to { id: idwyc} and in your controller change public ActionResult Test(int selectedWycieczka) to public ActionResult Test(int id).

Why your other methods are working is unclear to me.

Upvotes: 1

Related Questions