views:

138

answers:

2

Hi,

I've got a list of objects in an MVC view, each with a set of submit buttons - Move up, Move down and Remove.

Using the answer from this question, I can get the button clicked - but I need to know which item it would be operating on.

The problem comes from the fact the input's value attribute is passed back, I need more information than that - i.e. an id.

Below is a snippet of the contents of a for loop, rendering each item.

<ul>
    <li><input type="submit" name="SubmitButton" value="Move-Up" class="linked-content-position-modifier" /></li>
    <li><input type="submit" name="SubmitButton" value="Move-Down" class="linked-content-position-modifier" /></li>
    <li><input type="submit" name="SubmitButton" value="Remove" class="linked-content-modifier" /></li>
    <li><%= Model.Contents[i] %></li>
</ul>

When the form is posted back, I can see that the SubmitButton has a value of either Move-Up, Move-Down or Remove - but no idea which item in the array it's referring too.

Without changing the value to something really ugly, how would I tell which item it's referring to?

A: 

Just a quick idea, but maybe you could try adding hiddenfield to each row and see which one gets submitted?

Rob
A: 

Why are you using a button?

Could a link work instead?

<a href="controller/MoveUp/<%= Model.Id %>">Move Up</a>

I use similar code to update items in a table, but I do it with javascript and update the table accordingly...

<script type="text/javascript">
    $('img.action').click(function(){
        var itemId = this.attr('id'); // Do some parsing of the id here, i use a format of bi-##

        $.ajax({ uri: 'controller/MoveUp/' + itemId });
    });
</script>
Krisc
It needs to account for people with Javascript disabled, so the information needs to be posted because the user could have made other changes that need to be maintained.
Kieron
So while JS is out, a link should still work. It can execute (get) to the controller with an ID attached like in my example. The action can do the move work and return the original view. This time it will have the updated sort order. Sorry if I wasn't clear that my two examples were separate ideas.
Krisc

related questions