MVC3 C#实体框架基于下拉列表选择填充文本框 [英] MVC3 C# Entity Framework populate textbox based off dropdownlist selection

查看:181
本文介绍了MVC3 C#实体框架基于下拉列表选择填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个链接我试过,这导致我的代码...这不工作:D

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

获取DropDownLists上的Drop
创建级联下拉列表

我试图允许用户从下拉列表中选择一个零件号(itemnmbr),并在其选择后,让页面刷新具有正确值的部分描述(itemdesc)文本框。

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.

查看代码

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

Firefox Error Console returns:


时间戳:12/28/2012 2:40:29 PM警告:不推荐使用属性指定的
属性。它总是返回true。源文件:
http://ajax.aspnetcdn。 com / ajax / jquery / jquery-1.6.4.min.js 行:2

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

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返回那两个,以及下面(它们位于上述两个之间):

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


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

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


推荐答案

我认为你在正确的轨道上。请查看有关如何使用get()的此页面上的示例。

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

猜测GetPartDesc从不受到打击,或者没有获得您期望的参数。如果您更改,可能会工作:

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

但是我没有测试过。另外我个人也使用jquery .ajax方法来做这种事情。我从来没有使用过,虽然阅读一点似乎你应该工作。无论如何,您可以尝试以下方式:

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

这篇关于MVC3 C#实体框架基于下拉列表选择填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆