提交后显示和隐藏带过滤内容的表(jQuery)

时间:2015-08-06 07:36:42

标签: javascript jquery asp.net-mvc-4 submit

Szenario 1 我有一个视图,其中存在表格和表格。 通过我的jQuery代码,“table id =”divEdit“”开始隐身,应该出现,当我“提交id =”btnEdit“”形式:

$(function () {
$("#divEdit").hide();

$("#btnEdit").click(function (e) {
    e.preventDefault()
    $("#divEdit").show();
});
});

一开始它就是一切都很棒,桌子是不可见的,我可以在我的表格中插入一些东西,这是一个搜索功能,它有两个输入来过滤我的表格内容。

表单的值将发送到我的索引控制器,该控制器返回表的内容。 的控制器:

public ActionResult Index(string Product, string searchString2)
    {
        //List of all Products
        List<SelectListItem> product = new List<SelectListItem>();
        Productlist(product);
        ViewBag.Product = new SelectList(product, "Value", "Text");

        //Compares the inserted Product and S_N with the ProductName and Serial in the DB Tabel Products
        var newproducts = from n in db.Products select n;
        if ((!String.IsNullOrEmpty(Product)) && (!String.IsNullOrEmpty(searchString2)))
        {
            newproducts = newproducts.Where(n => n.ProductName.Contains(Product) && n.Serial.Contains(searchString2));
            //Saves the Data in DB NewProducts so you dont have to insert it again if the product doesnt exists 
            db.NewProducts.Add(new NewProduct { Product = Product, S_N = searchString2 });
            db.SaveChanges();
            ViewBag.ID = db.NewProducts.Max(d => d.id);

        }

        return View(newproducts.ToList()); 

    }

但是当我提交表格时,会显示表格的全部内容(未经过滤)。提交在第一次点击后有一个男性功能,根本不做任何事情。我将新值插入表单后,表格不会更新。

Szenario 2

我发现e.preventDefault()是停止更新的人,因此我对其进行了修改,现在我的skript看起来像这样:

$(function () {
$("#divEdit").hide();

$("#btnEdit").click(function () {
    $("#divEdit").show();
});
});

但那不是我的问题的解决方案。现在他更新了我的桌子,但是

  1. 我只能看一下表格

  2. 当我第二次在第二次使用提交时,他向我展示了包含整个内容的整个表格(未经过滤的内容),我必须第二次点击它以查看表格中的过滤内容< / p>

  3. 你知道如何解决这个问题吗?

    提前致谢。

    查看:

    @model IEnumerable<Escalationmanagementtool.Models.Product>
    @section Scripts {
    
     <script src="~/Scripts/GetCustomers.js" type="text/javascript"></script>
    
     }
    @{
    ViewBag.Title = "Index";
    }
    
    
    @using (Html.BeginForm())
    {
    <p>
    
        Product: @Html.DropDownList("Product", "Select Product")
        Serial number: @Html.TextBox("SearchString2")  
    
        <input type="submit" value="Search" id ="btnEdit" runat="server"/></p>
    
    }
    
    
    
    <table id ="divEdit" runat="server" style= "display:none">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Customer.Name)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Customer.Name)
        </td>
        <td>
            @Html.ActionLink("Choose", "Chosen", "New", new { id=item.Id, idc = item.Customer }, null) 
         </td>
    </tr>
    }
    
    </table>
    
    
    @Html.ActionLink("Search", "Search", "Add", new { ID = ViewBag.ID }, null) 
    

2 个答案:

答案 0 :(得分:0)

上述要点的问题可能如下 -

1.(我可以看到桌子只有一秒钟)

此处页面再次刷新,执行该行 - $( “#divEdit”)隐藏()。 因此它会在执行该行后隐藏表格,并且在执行该行之前只能查看表格。

2.(当我在第二次使用提交时,他向我展示了整个表格的内容(未经过滤的内容),我必须第二次点击它以查看表格中的过滤内容)

这里当您第一次提交执行的行时 -     $(“#btnEdit”)。click(function(){     $( “#divEdit”)显示()。 }); 因此,只有表格显示并且尚未提交任何表格来过滤表格,只有当您再次点击它时才会过滤。当您第二次单击提交表单以过滤表时..

希望这有助于解决您的问题..

答案 1 :(得分:0)

我发现另一个带有ajax的解决方案,在点击提交后首先显示该表。因此我使用了Azim Zahir的教程。 您可以在http://www.codeproject.com/Tips/886473/Implementing-AJAX-in-ASP-NET-MVC上找到它。 在我没有得到有用的回应后,我寻找其他解决方案,而没有写我自己的skript。获得的经验:不要求解决一个问题;)。

我希望它能帮助您找到适合您的Webtool的解决方案。

相关问题