Nathan R
Nathan R

Reputation: 870

How to pass a value from underscore.js to the MVC Controller?

Using underscore.js in my View, I'm trying to pass the variable serialNumberId back to my view. Here's what I tried:

<a href="@Url.Action("process", "wip", new { id = <%= serialNumberId %> })">
    Text
</a>

...but it didn't like that. Red squiggles.

Then I tried:

<a href="@Url.Action("process", "wip")/<%= serialNumberId %>">
    Text
</a>

...but then the code never even reached my process action method in the controller.

Everything works fine if I hardcode a serialNumberId value like this:

<a href="@Url.Action("process", "wip", new { id = 10 })">
    Text
</a>

or like this:

<a href="@Url.Action("process", "wip")/10">
    Text
</a>

So how do I do THAT, but with my serialNumberId variable instead?

Edit: Here's the code where serialNumberId is coming from (it's part of a DataTables function):

...
columns: [
    {
        data: 'serialNumberId', searchable: false, orderable: false,
        render: function (data, type, row, meta) {
            var structure = _.template($('#tmpl-actions').html()),
                html = structure({ serialNumberId: data });

            return html;
        }
    },

Edit 2: Okay, so after some more playing around, I don't think this is an underscore problem. This is a datatables problem. serialNumberId: data above is Null for some reason. When I step through the ASP.Net it's being filled with an int, but by the time it goes through DataTables it's empty.

Upvotes: -1

Views: 236

Answers (1)

Salar Afshar
Salar Afshar

Reputation: 249

if serialNumberId is jquery variable you can't use it with razor syntax. but if you want create url with it you can do this `

<script>               
    $("a").click(function(){
         var url='@Url.Action("process","wip")'+serialNumberId;
         window.location.href=url;
    });
</script>

Upvotes: 0

Related Questions