0

This is my controller action...

    // GET: ex: /Question/Details/5?project=1
    public ActionResult Details(int? project, int? questionId)
    {
      ...
    }

This is my Knockout VM:

    // Get some other info about question
    $(document).ready(function () {
            getSomeOtherInfo(<need to pass question id here>, <need to pass project id here>);
    });

Any help will be greatly appreciated.

fluidguid
  • 1,511
  • 14
  • 25

1 Answers1

0

If you are using a strongly typed ViewModel for Details, you can add the two properties into the view model, then in the view provide the values to JavaScript:

$(document).ready(function () {
    getSomeOtherInfo(@(Model.project), @(Model.questionId));
});

If you are not using a strongly typed ViewModel, you can use the ViewBag

Andy T
  • 10,223
  • 5
  • 53
  • 95
  • Thanks Queti, I tried your option. When I use getSomeOtherInfo(@(Model.project), @(Model.questionId)); I get an error "invalid character '@'" and when I use getSomeOtherInfo(Model.project, Model.questionId); I get an error 'Model' is undefined. I understand I must be wrong somewhere in implementing your help, but where? – fluidguid Feb 07 '13 at 22:07
  • If you are using Razor view engine, then the @(Model.project) would work. If you are not, then you would do <%= Model.project %> – Andy T Feb 07 '13 at 22:10
  • Found the answer why didn't it work for me. I was writing it in my js file. Below link explains in detail. http://stackoverflow.com/questions/7626662/access-a-model-property-in-a-javascript-file – fluidguid Feb 13 '13 at 02:40