问题描述:

I have a @Html.textBox which is updating my view with a table (without refreshing the entire page), when a search-button is pressed. I am using Ajax to do this and it works fine.

My problem is that I want to dynamically update the table, without having to press the button.

For example, if I type "ordernumber 1" I want the table to dynamically update the table view with everything that starts with "ordernumber 1".

Should I use an If-statement somewhere that dynamically checks the textbox input or is there a better solution? :)

My code:

Controller:

 private LogDBContext db = new LogDBContext();

// GET: LogModels

//Adds parameters to search and order data from the database

public ActionResult Index(string searchString, string currentFilter, int? page, string sortOrder)

{

ViewBag.CurrentSort = sortOrder;

string myID = "0";

if (Session["myID"] != null)

{

myID = Session["myID"].ToString();

}

else

{

return Redirect("/");

}

int testID;

try

{

testID = Convert.ToInt32(myID);

}

catch (FormatException ex)

{

throw new FormatException("FormatException error in LogModelsController", ex);

}

if (searchString != null)

{

page = 1;

}

else

{

searchString = currentFilter;

}

ViewBag.CurrentFilter = searchString;

//Linq code that gets data from the database

var message = (from log in db.Log

where log.customerId == testID

select log);

//message = message.Where(q => q.customerId.Equals(myID));

if (!String.IsNullOrEmpty(searchString))

{

message = message.Where(s => s.message.Contains(searchString));

}

//PageSize displays the maximum number of rows on each page

int pageSize = 10;

int pageNumber = (page ?? 1);

return View(message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize));

}

[HttpPost]

public PartialViewResult LogPartialView(string searchString, string currentFilter, int? page, string sortOrder)

{

ViewBag.CurrentSort = sortOrder;

string myID = "0";

if (Session["myID"] != null)

{

myID = Session["myID"].ToString();

}

int testID;

try

{

testID = Convert.ToInt32(myID);

}

catch (FormatException ex)

{

throw new FormatException("FormatException error in LogModelsController", ex);

}

if (searchString != null)

{

page = 1;

}

else

{

searchString = currentFilter;

}

ViewBag.CurrentFilter = searchString;

//Linq code that gets data from the database

var message = (from log in db.Log

where log.customerId == testID

select log);

if (!String.IsNullOrEmpty(searchString))

{

message = message.Where(s => s.message.Contains(searchString));

}

//PageSize displays the maximum number of rows on each page

int pageSize = 10;

int pageNumber = (page ?? 1);

return PartialView("_LogPartialLayout", message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize));

}

}

HTML-code: LogLayout

 @using (Ajax.BeginForm("LogPartialView", "LogModelsController",

new AjaxOptions

{

HttpMethod = "POST",

InsertionMode = InsertionMode.Replace,

UpdateTargetId = "divLogs"

}, new

{

id = "NewTableId"

}))

{

<p>

<b>Message:</b> @Html.TextBox("SearchString")

<input type="submit" id="submit" name="submit" value="Search" />

</p>

}<div id="divLogs">

@RenderBody()

@Html.Raw(ViewBag.Data)

</div>

HTML-code - PartialView:

@model PagedList.IPagedList<namespace.Models.LogModel>

@using PagedList.Mvc;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

<table class="table">

<tr>

<th>

@Html.ActionLink("message", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

</th>

<th>

@Html.ActionLink("timestamp", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

</th>

<th>

@Html.ActionLink("level", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

</th>

</tr>

@foreach (var item in Model)

{

<tr>

<td>

@Html.DisplayFor(modelItem => item.message)

</td>

<td>

@Html.DisplayFor(modelItem => item.timeStamp)

</td>

<td>

@Html.DisplayFor(modelItem => item.level)

</td>

</tr>

}

</table>

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",

new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

相关阅读:
Top