Question: I need to display an input button on the success message in my view. I am working in MVC 3 application using razor views. This button will allow the user to navigate to another view.

### Controller.

```
var successfull = new string[]
{
  "All " + builder.Data.Count.ToString() + " work items were added successfully."
};

return new JsonResult
{
   Data = new { success = true, msg = successfull}
};

```

### JavaScript.

```
var jsonText = JSON.stringify(json);

$.ajax({
   url: '/Builder/CreateWork',
   type: 'POST',
   dataType: 'json',
   data: jsonText,
   contentType: 'application/json; charset=utf-8',
   success: function (result) {
     // clear table
     $('#instruments-data').empty();
     resultMessage(result);
   },
   complete: function () {
     // hides the loading gif
     $('.loading').hide();
   }
});

```

### View

```
<div id="resultMessage"></div>

```

Is there a way to add to the ajax code to include the following input button.

```
<input type="button" class="styledbutton" value="Some text" onclick="window.location.href='Url.Action("action", "controller")';" />

```

### EDIT ---

The problem lies with this piece of code - but can't see the problem.

```
        onclick="window.location.href = '@Url.Action("actionName", "controllerName")'"/>');

```

Please advise.

Answer: 
It looks like you're re-querying for the `select` element, and always getting the first one when you supply the data. The jQuery [`val`](http://api.jquery.com/val/) method will give you the value of the first item in the selected list. You can use `this` to supply context to jQuery to get *only* the one you want.

But, by doing that, you'll need to look up to the nearest row, then back down to the `product` tag to find that product id.

Try something like this:

```
:javascript
  $(function(){
    $('.select').change(function(){
      var quantity = $('select', this).val();
      var product_id = $('product', $(this).closest('.row')).attr('id');
      $.ajax({
        url: "#{update_cart_path(format: :js)}",
        type: 'post',
        data: { quantity: quantity, id: product_id },
        dataType: 'script',
      })
    });
  });

```

This can be cleaned up a bit by being more strategic about where you put your change listener, or by using data attributes to put the information in a more convenient place.
this looks wrong: `$("select").val(), id: $("product")` 

`$("select")` will match the first select on the page, and `$("product")` will look for a tag like `<product>`, which i don't think exists anywhere.

In the first instance, "quantity" you probably want to say 

`$(this).val()`

where `$(this)` is the element that's had the change event put on it, ie the select that's been changed.

For `$("product")`, i think it should be `$("#product")`: the # means "with id equal to".

Also, is the url of the ajax call coming out right? Maybe it needs to be in `<%= %>` rather than `#{}`. 

Also, you have a trailing comma at the end of the options object in your ajax call. (after 'script')