I have two collections of the same type of items on the same page that currently use the same Moustach HTML template for rendering. The template contains a Remove button.
The problem is that Remove in each context needs to perform something different (essentially based on which div it is currently in).
My reluctance to use two templates is that I'm trying to stick to a one-to-one relationship between DataContract objects and templates so that an item is always displayed consistently in the UI.
The forms are going to submitted via ajax.
What is the best option for handling this?
Create another template (really want to avoid doing this)? Add a class name to each button?
EDIT:
Given this class:
public class MyDataContract()
{
public int Id { get; set; }
public string MyString { get; set; }
}
Using this template:
<form id={{Id}} action="/dragsource/action/">
<p>{{MyString}}</p>
<input type="button" name='command' value='Remove' />
<input type="button" name='command' value='Save' />
</form>
I load two different lists of MyDataContract.
I have two <divs> on the page, for argument sake call them dragsource and droptarget.
The button in the form needs to perform two different things based on the div it's in.
if the default action in the template is something like:
action='/dragsource/action/'
I'm thinking that I could change the dropped form action inside the drop handler to something like:
drop: function(event, ui) {
$(ui.draggable).parent().attr('action', '/controllername/droptarget/')
$(ui.draggable).appendTo(this);
}
Then my MVC action method can look something like:
public ActionResult Droptarget(MyDataContract myDataContract, string command)
{
if(command = "Remove")
{
// Do stuff
}
else
{
// Do stuff
}
}
Or is there a better for succinct way to doing this?