1

I currently have panels and each panel has an ID associated with it which is held in the data-id attribute of each panel. I can get this easily through JavaScript. One the panels I have a delete button which will delete the panel and it's entry within the database. Once this delete button is clicked, a modal will fire up with a Html.BeginForm() which is hooked up to an action on my controller. I was wondering if it was possible to add an the widget ID to the routing values via JavaScript?

Here is my modal:

<div class="modal fade" id="deleteWidgetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Delete widget?</h4><!--add depending on which panel you have clicked-->
        </div>
        <div class="modal-body" id="myModalBody">
            <!--Depending on which panel insert content-->
            @using (Html.BeginForm("DeleteWidgetConfirmed", "Dashboard", FormMethod.Post,))
            {
                @Html.AntiForgeryToken()

                <div class="form-horizontal">
                    Do you wish to delete this widget?

                    <div class="form-group">
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                            <button type="submit" value="DeleteWidgetConfirmed" class="btn btn-danger btn-ok" id="delete-widget">Delete</button>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>

and here is the JavaScript:

    /*---------------------------------------------DELETING PANELS-------------------------------------------------*/
$(document).ready(function () {
    $('#columns').on('click', '.glyphicon.glyphicon-trash', function (event) {
        //get id here

        //toggle the modal
        $('#deleteWidgetModal').modal('toggle');

        //pass id to form
    });
});

$(document).ready(function () {
    document.getElementById('#delete-widget').onclick = function (event) {
        event.preventDefault();

        var parentElement = $(this).closest(".col-md-4.column");
        var targetElement = $(this).closest(".panel.panel-default");
        targetElement.remove();

        //parentElement.addClass("expand-panel");
        checkEmptyPanelContainers();
    }
});
1
  • can you put some code that helps to understand your need ? Commented Jun 2, 2015 at 15:04

1 Answer 1

1

Html.BeginForm is transformed into a <form> tag before your javascript and user interaction is executed. You can modify that form using something like this

$("form").prop('myattribute', value)
Sign up to request clarification or add additional context in comments.

3 Comments

do you know what myattribute would be in my case? for route value?
@Johnathon64 HTML Doesn't have a concept of routes/MVC. What you need to do is set the Action on the form. I'd suggest you use server-side code to output a sample route with known values, then substitute actual values in JS. Of course, if you're using multiple routes/need multiple styles you'd need to do each type individually which is less than ideal
action is the name of the property on the form tag

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.