0

For far too long I've been stumbling around trying to make a simple .click jquery method work in my c# site. I just want it to call to a function in my controller, but I can't seem to get any button click to register. The calls in it are very similar to a .change() function for another control that works just fine.

JQuery method in question:

$('#addUserButton').click(function () {
            var e = document.getElementById('searchDDL');
            var itemText = e.options[e.selectedIndex].text;
            var url = encodeURI('@Url.Action("AddUser")' + "?User=" + itemText);
        });

HTML of button that is intended to trigger the above function:

<button id="addUserButton" type="button" value="addUser">&gt;</button>

Some things I've researched and tried already:

  • Changing from a .click() method to a .live("click", etc) method made no difference.
  • I saw that setting the button type="submit" would be a problem, so I changed it to "button."
  • I made sure to include it in document.ready() - I was already doing this before researching other problems.

I have created a jsfiddle for the problem here. I had to remove a lot of sensitive information so the page is not fully fleshed out. It is for an internal application and only has to work with IE.

EDIT: Below I am adding the function I am trying to call in my controller; other posters have verified that the jquery is working as expected, so I'm inclined to believe it is something to do with my c#.

public ActionResult AddUser(string User)
    {
        //numUsers = App.NumberUsers + 1;
        selectedUsers.Add(User);
        return RedirectToAction("ApplicationForm");
    }
3
  • 4
    The fiddle works for me when I include jQuery. Commented Jul 8, 2013 at 14:22
  • Your jsfiddle contains @Url.Action() references in the JavaScript. Can you at least confirm that these are populated with proper URLs when you're running this in your live environment? Commented Jul 8, 2013 at 14:36
  • @AdrianWragg - I added alerts and verified that, yes, the @Url.Action() references are populated with URLs. Relative URLs, but valid URLs. Commented Jul 8, 2013 at 15:12

3 Answers 3

1

Did you try debugging your function or even putting alerts in it to flesh out whats happening.

$('#addUserButton').click(function () {
alert("FUNCTION IS GETTING CALLED ON CLICK EVENT");
            var e = document.getElementById('searchDDL');
alert(e);
            var itemText = e.options[e.selectedIndex].text;
alert(itemText);
            var url = encodeURI('@Url.Action("AddUser")' + "?User=" + itemText);
alert(url);
        });

Doesn't look like your function makes an AJAX request after getting the URL.

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

3 Comments

I did add alerts to debug and verified that the function is getting called and accessed as expected. As you pointed out, I never made a call with the url; I added $("contentDiv").load(url); like in my other function. Unfortunately it still isn't firing off the function. Thanks for the help.
@rgreasons - $("#contentDiv").load(url); you need a # before your ID
^sure enough. I would have swore I put one in there since it was copy and paste. ugh. Thanks.
0

It's not something silly like the extra closing > bracket on this?

<button id="addUserButton" type="button" value="addUser">></button>

1 Comment

Sorry, I forgot to update that after another commenter mentioned the issue, but it appears he deleted his comment. But no, it wasn't the issue.
0

From your JSFiddle, it seems you're not including the jQuery.

Which is at the top-left corner of JSFiddle.

I've updated fiddle working fine. Remember you're including jQuery, so try to make use of its feature and leave Javascript.

Comments

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.