0

I have one last thing left on a project and its a doozy. Not only is this my first web application, but its the first app i used Jquery, CSS and MVC. I have no idea on how to proceed with this.

What i am trying to do is:

In my controller, a waste item is validated, and based on the results one of these things can happen. The validation is completed, nothing bad happens, which sets ViewData["FailedWasteId"] to -9999. Its a new waste item and the validation did not pass, which sets ViewData["FailedWasteId"] to 0. Its an existing waste item and the validation did not pass, which sets ViewData["FailedWasteId"] to the id of the waste item.

This ViewData["FailedWasteId"] is set on page load using

    <%=Html.Hidden("wFailId", int.Parse(ViewData["WasteFailID"].ToString()))%>

When the validations do not pass, then the page zooms (by window.location) to an invisible div, opens the invisible div etc. Hopefully my intentions are clear with this poor attempt at jquery.

The new waste div is and the existing item divs are dynamically generated (this i know works) ">

So my question here is... Help? I cant even get the data to parse correctly, nor can i even get the conditionals to work. And since this happens after post, i cant get firebug to help my step through the debugger, as the script isnt loaded yet.

        $(document).ready(function () {
            var wasteId = parseInt($('#wFailId').text());

            if (wasteId == -9999) {
                //No Issue
            }
            else if (wasteId < 0) {
                //Waste not saved to database
            }
            else if (wasteId == 0) {
                //New Waste
                window.location = '#0';
                $('.editPanel').hide();
                $('#GeneratedWasteGrid:first').before(newRow);
                $('.editPanel').appendTo('#edit-panel-row').slideDown('slow');
            }
            else if (wasteId > 0) {
                //Waste saved to database
            }
        });
John Stuart
  • 333
  • 8
  • 23

1 Answers1

3
var wasteId = parseInt($('#wFailId').text());

This line is wrong. It attempts to get the text contents inside the hidden input element, which is the empty string because no contents exist (e.g. "Hello" in <span id="foo">Hello</span>). This causes parseInt() to return the value NaN (Not a Number), causing every one of your conditions to fail but not causing any JavaScript exception to be thrown.

The value you are looking for instead exists as the attribute value of the element, retrievable using .val():

var wasteId = parseInt($('#wFailId').val(), 10); // or use:
var wasteId = +$('#wFailId').val();

(JavaScript's unary plus operator is what I would use to cast to a number if necessary. Note that in any case, it is bad practice to use parseInt() without specifying the base/radix 10, because otherwise 010 is interpreted as an octal number: 8 in decimal.)

PleaseStand
  • 31,641
  • 6
  • 68
  • 95