Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax mvc搜索数据时,如何在querystring上保留id_Javascript_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript 使用ajax mvc搜索数据时,如何在querystring上保留id

Javascript 使用ajax mvc搜索数据时,如何在querystring上保留id,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我正在使用ajax创建搜索页面 场景是:在我们的购买系统中,用户从dropdownlist中选择类别,然后单击browse按钮显示一个模式表单,其中包含基于所选类别的产品列表 问题是在显示模式后,然后用户填写搜索条件并单击搜索,categoryID为空。在使用ajax搜索数据时,如何保持该值 以下代码表示我迄今为止所做的工作: 购买视图: @using (Html.BeginForm("Create", "Invoice", FormMethod.Post, new { @enctype = "

我正在使用ajax创建搜索页面

场景是:在我们的购买系统中,用户从dropdownlist中选择类别,然后单击browse按钮显示一个模式表单,其中包含基于所选类别的产品列表

问题是在显示模式后,然后用户填写搜索条件并单击搜索,categoryID为空。在使用ajax搜索数据时,如何保持该值

以下代码表示我迄今为止所做的工作:

购买视图:

@using (Html.BeginForm("Create", "Invoice", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
    @Html.LabelFor(model => model.CategoryID, new { @class = "control-label col-md-2" })
    @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.Categories, "CategoryID", "Name"), "-- Please Select --", new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.CategoryID)

    @Html.LabelFor(model => model.ProductID, new { @class = "control-label col-md-2" })
    @Html.HiddenFor(model => model.ProductID)
    @Html.EditorFor(model => model.Product.Name, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
    <button class="btn btn-primary" id="btnLookupProduct" data-id="@Model.CategoryID" data-toggle="modal" data-target="#myModal">Lookup Product</button>
}

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Product List</h4>
                @using (Ajax.BeginForm("Search", "Product", 
                    new AjaxOptions
                    { 
                        HttpMethod = "POST",
                        InsertionMode = InsertionMode.Replace,
                        UpdateTargetId = "lookup-timekeeper-container" 
                    }))
                {
                    @Html.DropDownList("FilterField",
                        new List<SelectListItem>
                        { 
                            new SelectListItem { Text = "Code", Value = "Code" },
                            new SelectListItem { Text = "Name", Value = "Name" }
                        },
                        "-- Please Select --",
                        new { @class = "form-control" })

                    @Html.TextBox("FilterValue", null, new { @class = "form-control", @placeholder = "Enter keyword" })

                    <input type="submit" value="Search" class="btn btn-primary" />
                }
            </div>
            <div class="modal-body" id="lookup-timekeeper-container">
                // list of product
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnLookupProduct").click(function () {
                var url = '@Url.Content("~/Product/Search/")' + $("#CategoryID").val();
                $.get(url)
                    .done(function (data) {
                        if (!data.message) {
                            $("#lookup-timekeeper-container").html(data);
                            $("#myModal").modal(show = true, backdrop = true);
                        } else {
                            alert(data.message);
                        }
                    });
            });
        });
    </script>
}
产品部分页:

@model List<PurchaseSystem.Models.Product>

<div class="table-responsive">
    <table class="table table-striped table-hover table-condensed">
        <tr>
            <th>Code</th>
            <th>Name</th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.Code)</td>
                <td>@Html.DisplayFor(modelItem => item.Name)</td>
            </tr>
        }
    </table>
</div>

将下拉列表中的值设置为HiddenField

<input type="hidden" value="" name="hiddens" id="hiddens" />

$("#btnLookupProduct").click(function () {
    var DropValues = $("#DROPDOWNID").val();
    document.getElementById("hiddens").value = DropValues;
});

现在单击搜索,将此隐藏字段值放入变量中使用它

类别ID的标记在第一种形式中定义,因此不会在第二种形式中发回form@StephenMuecke你说得对,这就是问题所在,但我不知道如何解决它。我无法将categoryID移动到第二个表单,因为在打开第二个表单之前第一个表单中需要它。在第二个表单中添加一个@Html.HiddenFormodel=>model.categoryID,并使用$'btnLookupProduct'。单击函数{$'secondForm categoryID'.val$'firstForm categoryID'.val;};单击“查找产品”按钮时设置CategoryID。您可以查看如何使用将下拉列表与第二个表单关联,但我的建议是去掉Ajax.BeginForm垃圾,并对已经在另一部分使用jquery的部分使用jqueryfunction@cackharot我无法以第二种形式添加@Html.HiddenFormodel=>model.CategoryID,这是因为在第一次加载页面时,category dropdownlist上没有数据。我是这样做的:1在var url='@url.Content~/Product/Search/'+$CategoryID.val之前,我将其放在第二个表单2上;我添加了var categoryID=$categoryID.val;document.getElementByIdhidCategoryID.value=categoryID;categoryID仍然为空一件事你需要把它放到Purchase视图中,第二件事你需要把这个值传递给带有参数的公共ActionResult搜索函数,然后你会在部分视图中找到它,并将它再次映射到部分视图中的新hiddenfield i.e@Html.HiddenHiddenFFFDData,somevarialfrompage或@Html.HiddenForm=>m.somevarialfrompage我明白了@隐藏将不起作用,但会起作用。现在问题解决了,谢谢
<input type="hidden" value="" name="hiddens" id="hiddens" />

$("#btnLookupProduct").click(function () {
    var DropValues = $("#DROPDOWNID").val();
    document.getElementById("hiddens").value = DropValues;
});