0

I want to pass a value from the view to the controller the value is retrieve via jquery when I use the code below it does not work the value is null in the controller.

  //Hardcoded to test   
  var groupId = 1;
    $('#timeList').load('/Time/Index/' + groupId);

    public ActionResult Index(int? groupId)
    {
        AddProjectToViewData(-1);
        return View(_service.ListTimes());
    }

3 Answers 3

1

Try this:

var groupId = 1;
$('#timeList').load('/Time/Index', {groupId: groupId}); //Send named parameter

[HttpGet]
public ActionResult Index(int? groupId)
{
    AddProjectToViewData(-1);
    return View(_service.ListTimes());
}

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

assuming that you are using default routes in global.asax.cs you can retrieve this value in id

public ActionResult index(int? id)
{
     AddProjectToViewData(-1);
     return View(_service.ListTimes());

}

if you do wish to receive it in groupid you have to change jquery code a little //Hardcoded to test

var groupId = 1;
    $('#timeList').load('/Time/Index?groupid=' + groupId);

Comments

0

You want to do either a post or a get, and I think you may want to pass back a string or json. For ease, I'll show you a get with json:

///your javascript

var groupId =1;

$.get('Time/Index/' + groupId, function(data) {
  $('#timeList').val(data);
   ///might need to do a for each since you are returning a list or whatever
});

///your controller logic

[HttpGet]
public string Index(int? groupId)
    {
        AddProjectToViewData(-1);
        //might want to pass back json here
        return WhateverYourDataIs.ToString();
    }

Hope this helps

2 Comments

how does it relate to the question? i wouldn't give it -1 though!
His question specifically states "pass a value from the view to the controller" - that's how it relates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.