2

Couple of links I've tried, that led me to my code... which isn't working :D

Get The Drop on DropDownLists and Creating Cascading Dropdown Lists

I am trying to allow the user to select a part number (itemnmbr) from a dropdown list and upon their selection, have the page refresh the part description (itemdesc) textbox with the correct value. Below is the closest I've gotten.

VIEW CODE:

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $("#ITEMNMBR").change(function () {
            $.get("/PartsLabor/GetPartDesc", $(this).val(), function (data) {
                $("#ITEMDESC").val(data);
            });
        });
    });
</script>

@using (Html.BeginForm()) 
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Add part to call: @ViewBag.CALLNBR</legend>

            @Html.LabelFor(model => model.ITEMNMBR, "Item Number")
            @Html.DropDownList("ITEMNMBR", (SelectList) ViewBag.Items, "Please Select a Part #")
            @Html.ValidationMessageFor(model => model.ITEMNMBR)
        <br />
            @Html.LabelFor(model => model.ITEMDESC, "Description")
            @Html.EditorFor(model => model.ITEMDESC)
            @Html.ValidationMessageFor(model => model.ITEMDESC)
        <br />
            <input type="submit" class="submit" value="Add Part" />

    </fieldset>
}

Controller Code:

    [Authorize]
    public ActionResult PCreate(string call)
    {

        var q = db.IV00101.Select(i => new { i.ITEMNMBR});
        ViewBag.Items = new SelectList(q.AsEnumerable(), "ITEMNMBR", "ITEMNMBR");
        ViewBag.CALLNBR = call;
        return View();
    }

    public ActionResult GetPartDesc(char itemnmbr)
    {
        var iv101 = db.IV00101.FirstOrDefault(i => i.ITEMNMBR.Contains(itemnmbr));
        string desc = iv101.ITEMDESC;
        return Content(desc);
    }

Firefox Error Console returns:

Timestamp: 12/28/2012 2:40:29 PM Warning: Use of attributes' specified attribute is deprecated. It always returns true. Source File: http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.4.min.js Line: 2

Timestamp: 12/28/2012 2:40:34 PM Warning: Use of getAttributeNode() is deprecated. Use getAttribute() instead. Source File: ~/Scripts/jquery-1.6.4.min.js Line: 3

Firefox Web Console returns those two, as well as the below (which lands between the above two):

Request URL: ~/PartsLabor/GetPartDesc?002N02337 Request Method: GET Status Code: HTTP/1.1 500 Internal Server Error

8
  • 1
    Can you elaborate what exactly is not working? Do you get any errors in your browser's debug tool console output window for example? Commented Dec 28, 2012 at 14:13
  • if you put a break point in GetPartDesc does it get hit? Does itemnmbr have the value you expected? Commented Dec 28, 2012 at 15:06
  • @BenTidman Put in a break point on the controller at GetPartDesc. Doesn't break when I select the itemnmbr... Commented Dec 28, 2012 at 18:57
  • @FrançoisWahl Using Firefox. When I look in "Web Console" and select a part, I get: [13:58:36.938] Use of attributes' specified attribute is deprecated. It always returns true. @ ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.4.min.js:2 [13:58:37.007] GET localhost:10659/PartsLabor/… [HTTP/1.1 500 Internal Server Error 64ms] -- [13:58:48.385] Use of getAttributeNode() is deprecated. Use getAttribute() instead. @ localhost:10659/Scripts/jquery-1.6.4.min.js:3 Commented Dec 28, 2012 at 19:03
  • Did you try the change I posted below? Commented Dec 28, 2012 at 19:03

1 Answer 1

1

I think you are on the right track. Check out the examples on this page on how to use get().

guessing GetPartDesc is never getting hit or it's not getting the parameter that you are expecting. It will probably work if you change:

        $.get("/PartsLabor/GetPartDesc", $(this).val(), function (data) {
            $("#ITEMDESC").val(data);
        });

to:

        $.get("/PartsLabor/GetPartDesc", { itemnmbr: $(this).val() }, function (data) {
            $("#ITEMDESC").val(data);
        });

But I haven't tested it. Also I personally use the jquery .ajax method for this kind of thing. I've never used get, though reading a little seems like what you have should work. Anyway you can try something like this:

    $.ajax({
            url: '/PartsLabor/GetPartDesc',
            data: { itemnmbr: $(this).val() }
        }).done(function (data) {
            $("#ITEMDESC").val(data);
        });
Sign up to request clarification or add additional context in comments.

10 Comments

Replaced everything between <script type="text/javascript"> and </script> with the $.ajax code. Now I receive the below on page load as well as the two java error at bottom of OP. Don't get the 500 Internal Error now. [15:10:29.349] TypeError: e.nodeName is undefined @ ~/Scripts/jquery-1.6.4.min.js:3
hmm... that's strange. Sounds like you have a different problem now. Does the page load at all? are you able to hit your break point now?
The page loads with the e.nodeName java error. The other two get thrown when I select something from the dropdown, then click OFF the dropdown (anywhere else on page). Did not stop at my breakpoint.
Hmm, now there's some error detail... Loaded Firebug, and that 500 Internal Server Error is giving me: Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'itemnmbr' of non-nullable type 'System.Char' for method 'System.Web.Mvc.ActionResult GetPartDesc(Char)' in 'TechConnect.Controllers.PartsLaborController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
make your parameter on your controller action nullable by decalring it as char? itemnmbr. Add logic to see if it HasValue before using it.
|

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.