【问题标题】:MVC3 C# Entity Framework populate textbox based off dropdownlist selectionMVC3 C# Entity Framework 填充基于下拉列表选择的文本框
【发布时间】:2012-12-28 13:42:29
【问题描述】:

我尝试了几个链接,这导致我找到了我的代码...这不起作用:D

Get The Drop on DropDownLists 和 Creating Cascading Dropdown Lists

我试图让用户从下拉列表中选择零件编号 (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

时间戳:2012 年 12 月 28 日下午 2:40:34 警告:getAttributeNode() 的使用是 已弃用。请改用 getAttribute()。源文件: ~/Scripts/jquery-1.6.4.min.js 行:3

Firefox Web 控制台返回这两个,以及以下(位于上述两者之间):

请求网址:~/PartsLabor/GetPartDesc?002N02337 请求方法:GET 状态码:HTTP/1.1 500 内部服务器错误

【问题讨论】:

  • 你能详细说明到底是什么不工作吗?例如,您是否在浏览器的调试工具控制台输出窗口中遇到任何错误?
  • 如果你在 GetPartDesc 中设置一个断点,它会被命中吗? itemnmbr 是否具有您预期的价值?
  • @BenTidman 在 GetPartDesc 的控制器上放置一个断点。当我选择 itemnmbr 时不会中断...
  • @FrançoisWahl 使用 Firefox。当我查看“Web 控制台”并选择一个部分时,我得到:[13:58:36.938] Use of attributes' specified attribute is deprecated。它总是返回 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 内部服务器错误 64ms] -- [13:58:48.385] 不推荐使用 getAttributeNode()。请改用 getAttribute()。 @localhost:10659/Scripts/jquery-1.6.4.min.js:3
  • 您尝试过我在下面发布的更改吗?

标签: c# jquery asp.net-mvc-3 entity-framework


【解决方案1】:

我认为你是在正确的轨道上。查看this page 上的示例,了解如何使用 get()。

猜测 GetPartDesc 永远不会被击中,或者它没有得到您期望的参数。如果你改变它可能会起作用:

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

到:

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

但我还没有测试过。我个人也使用 jquery .ajax 方法来处理这种事情。我从来没有用过get,虽然读一点似乎你应该工作。无论如何,你可以尝试这样的事情:

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

【讨论】:

  • 用 $.ajax 代码替换了 之间的所有内容。现在我收到以下页面加载以及 OP 底部的两个 java 错误。现在不要收到 500 内部错误。 [15:10:29.349] TypeError: e.nodeName 未定义 @ ~/Scripts/jquery-1.6.4.min.js:3
  • 嗯...这很奇怪。听起来你现在有一个不同的问题。页面是否加载?你现在能达到你的断点了吗?
  • 页面加载时出现 e.nodeName java 错误。当我从下拉列表中选择某些内容时,其他两个会被抛出,然后单击关闭下拉列表(页面上的任何其他位置)。没有停在我的断点处。
  • 嗯,现在有一些错误细节...加载了 Firebug,500 Internal Server Error 给了我:“/”应用程序中的服务器错误。参数字典包含“TechConnect.Controllers.PartsLaborController”中方法“System.Web.Mvc.ActionResult GetPartDesc(Char)”的不可为空类型“System.Char”的参数“itemnmbr”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
  • 通过将控制器操作上的参数标记为 char 来使其可以为空?项目nmbr。使用前添加逻辑看是否有 HasValue。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多