【问题标题】:How to keep id on querystring when search data using ajax mvc使用ajax mvc搜索数据时如何在查询字符串上保留id
【发布时间】:2014-08-20 03:37:44
【问题描述】:

我正在创建一个search page using ajax

场景是:在我们的购买系统中,用户从dropdownlist中选择类别,然后根据选择的类别选择click browse buttonshow a modal form with a list of product

问题出在显示模式之后,然后用户填写搜索条件并单击搜索,即categoryID is null。使用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>
}

采购总监:

public ActionResult Search(int? id, string filterField, string filterValue)
{
    var products = db.Products.Where(p => p.CategoryID == id);

    if (!string.IsNullOrEmpty(filterValue))
    {
        products = products.Where(filterField + ".Contains(@0)", filterValue);
    }

    return PartialView("_Lookup", products.ToList());
}

产品部分页面:

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

【问题讨论】:

  • 类别ID的&lt;select&gt;标签是在第一种形式中定义的,所以它不会在第二种形式中回发
  • @StephenMuecke 你说得对,这就是问题所在,但我不知道如何解决。我无法将 categoryID 移动到第二个表单,因为在打开第二个表单之前需要在第一个表单中使用它。
  • 在第二种形式中添加@Html.HiddenFor(model =&gt; model.CategoryID),并在单击查找产品按钮时使用$('#btnLookupProduct').click(function(){ $('#secondForm #CategoryID').val($('#firstForm #CategoryID').val()); }); 设置CategoryID
  • 您可以查看使用form attribute 将下拉列表与第二种形式相关联,但我的建议是摆脱Ajax.BeginForm 垃圾并使用jquery 来处理该部分(您已经在使用它对于其他功能)
  • @cackharot 我无法在第二种形式中添加@Html.HiddenFor(model =&gt; model.CategoryID),这会导致第一次加载页面时类别下拉列表中没有数据。

标签: javascript jquery ajax asp.net-mvc


【解决方案1】:

从 Drop Down 中取值到 HiddenField

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

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

现在点击搜索将这个隐藏字段值放入变量中使用它

【讨论】:

  • 这些是我做的:(1)我把&lt;input type="hidden" value="" name="hidCategoryID" id="hidCategoryID" /&gt;放在第二个表单上(2)在var url = '@Url.Content("~/Product/Search/")' + $("#CategoryID").val();之前我添加了var categoryID = $("#CategoryID").val(); document.getElementById("hidCategoryID").value = categoryID;并且categoryID仍然为空
  • 一件事你需要把它放在 Purchase View 中,其次你需要将该值传递给带有参数的公共 ActionResult Search() 函数,然后你会发现它再次映射到你的局部视图中局部视图中的新隐藏域
  • 即@Html.Hidden("hiddenFFfData", somevarialfrompage) 或@Html.HiddenFor(m => m.somevarialfrompage))
  • 好的,我明白了。 @Html.Hidden 不起作用,但 &lt;input type='hidden'&gt; 会。现在解决了,谢谢
猜你喜欢
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 2012-10-04
  • 2012-05-21
  • 2021-05-13
相关资源
最近更新 更多