0

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?

1 Answer 1

0

You can bind you action in parent, to each context... can you explain more about your problem? or add the code example?

EDIT:

I think you can create another Action in your controller, one to DroptargetRemove and one to DroptargetSave to call by ajax the actions.

if you're using form to submit you can change the action of form or create 2 forms.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I should have added code in the first place. Based on your comment I think I might have gotten a plan of action ('scuse the pun). If you can think of a better way of doing this I would appreciate your view. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.