2

我试过几个链接,这让我找到了我的代码......这是行不通的:D

获取 DropDownLists创建级联下拉列表

我试图让用户从下拉列表中选择一个零件号 (itemnmbr),并在他们选择后,让页面用正确的值刷新零件描述 (itemdesc) 文本框。下面是我得到的最接近的。

查看代码:

    <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>
}

控制器代码:

    [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 错误控制台返回:

时间戳:2012 年 12 月 28 日下午 2:40:29 警告:不推荐使用属性的指定属性。它总是返回 true。源文件: http ://ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.4.min.js行: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

4

1 回答 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);
        });
于 2012-12-28T15:14:36.673 回答