0

Hi what I want is to call a partial view from 'index.cshtml' passing it the model and some string input and when we get to the function 'ContactsTable' in the controller i want to get the model which of type List and to filter it's values and then send the new list to the partial view

After completeing this I want to use ajax to recall the partial view

I need help with accessing the model from the controller and the ajax function

Thank you

Ortal Levi
  • 11
  • 4
  • Uncomment `public string Search { get; set; }`. Delete that awful manual input and replace it with `@Html.TextBoxFor(m => m.Search )` and and then delete all your scripts and replace with `$('#Search ').change(function() { $('#ContactsTable]).load(@Url.Action(""), { search: $(this).val() }); });` and the method is `public ActionResult ContactsTable(string search) {` –  Sep 03 '17 at 11:40
  • But then How can I accsess the model (I mean the List) from this function:public ActionResult ContactsTable(string search) ? – Ortal Levi Sep 03 '17 at 11:51
  • Your don't (you not passing a `List` to that method and it would be crazy to event try). Inside the method you use `IQueryable contactsList = db.Contacts;` to call your database (and note you use `IQueryable`, not `List`). –  Sep 03 '17 at 11:55
  • And you should have a button to perform the search (as per the dupe), not handle the `.change()` event of the textbox. –  Sep 03 '17 at 11:56
  • But all the point here is not to have a button and show the result while typing, and go to db every type will be crazy – Ortal Levi Sep 03 '17 at 11:59
  • maybe there is a way to filter the list in the index file as i'm doing in public ActionResult ContactsTable func and then send the filtered data to the partial view but also maintain the orginial list until the page refresh – Ortal Levi Sep 03 '17 at 12:01
  • What is crazy is to attempt to pass back the whole collection back to a server method (which means you would need to generate hidden inputs with indexers for every property of every object in the collection. And if your initially displaying all objects, then you can just use javascript to filter it in the browser. –  Sep 03 '17 at 12:01
  • And do not change your original question and invalidate comments./answers. If you want to show additional code, then append it to your original question (I have rolled back your changes) –  Sep 03 '17 at 12:02
  • but is it possible to do my other suggestion? – Ortal Levi Sep 03 '17 at 12:04
  • Your saying that _go to db every type will be crazy_? That is not nearly as crazy as posting back the whole collection across the wire each time you press a key. If your initial view displays all `Contact` objects, then just use javascript/jquery to filter the html in the view (by showing/hiding rows that match the text in the textbox) –  Sep 03 '17 at 12:07
  • This is my first project in asp.net mvs so I didn't know, instead of hiding rows it doesn't better to rebuild the table? – Ortal Levi Sep 03 '17 at 12:09
  • [This article](https://www.w3schools.com/howto/howto_js_filter_table.asp) should give your a general idea of how to filter your table based on the search text (no unnecessary ajax calls back to the server) –  Sep 03 '17 at 12:16
  • Thank you very much – Ortal Levi Sep 03 '17 at 12:22

0 Answers0