0

I want to retrieve data in an html table using Ajax in ASP.net mvc but the success part is not executing and do not know how to show returned data in a table using Ajax. Please suggest any method to solve this issue. thanks..

Index.cshtml

@model IEnumerable<MvcApplication4.Models.tbl_product>
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <title>Index</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#Button1').click(function () {
                alert("button clicked");
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'Home/Index',
                    data: "{'searchString':'" + document.getElementById('searchString').value + "'}",
                    async: false,
                    Success: function (response) {
                        alert("Success");
                        window.location.reload();
                    },
                    error: function () { alert("error"); }
                });

            });
        });
    </script>
</head>
<body>

    @Html.TextBox("searchString");
    <input type="button" value="filter" id="Button1" />
    <table id="showData">
        @{Html.RenderPartial("SearchList");}
    </table>
</body>
</html>

HomeController.cs

 public class HomeController : Controller
    {
        //
        // GET: /Home/
        ProductEntities dbentity = new ProductEntities();
        public ActionResult Index()
        {
            return View(dbentity.tbl_product.ToList());
        }

        [HttpPost]
        public ActionResult Index(string searchString)
        {
            var query = dbentity.tbl_product.Where(c => c.ProductName.Contains(searchString));
            return View(query.ToList());
        }
    }

SearchList.cshtml

@foreach (var item in Model)
{ 
<tr>
<td>@item.ProductName</td>
<td>@item.ProductId</td>
<td>@item.ProductDesc</td>
</tr>
}
4
  • firstly, it is success not Success Commented Dec 9, 2013 at 11:15
  • what error is it throwing. Is it hitting action in controller Commented Dec 9, 2013 at 11:15
  • and also include dataType:'json'; in your ajax call Commented Dec 9, 2013 at 11:17
  • After changing to lowercase now the success part is executing but how will i append the data search in a table and can i call ajax function on OnBlur event rather than clicking on the button... Thanks for the above suggestions.... Commented Dec 9, 2013 at 11:18

1 Answer 1

1
  1. Its success not Success
  2. You need to return Partial View
  3. You need to updated table HTML with returned partial view

Change You code as

success: function (response) {
    alert("Success");
    $('#showData').html(response)
},

Controller Code

 return PartialView("SearchList", query.ToList());

If you are not supplying ViewName, by convention It will use ActionName as view name. thus pass SearchList as ViewName

EDIT:

Also, You need to pass model to render partial

@{Html.RenderPartial("SearchList", Model);}
Sign up to request clarification or add additional context in comments.

2 Comments

change the code, it is working but another textbox get generated when i click on Filter button. Why is this so.. Thanks..
Try return PartialView("SearchList", query.ToList());. You need to use SearchList partial view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.