I have a simple MVC controller which uses a function and returns a list of CardType. now in my view I just do a simple foreach loop and draw out each CardType that was return't, now I have filter textboxes on the view so they can input some text hit enter and the page will refresh will the new results. But I know I can use AJAX to do this, but i don't really have a clue on where to start or end :).
Here is my controller method
[HttpGet]
public ActionResult CardTypes(TypeSearchModel searchModel)
{
var cs = new CardService();
searchModel.CardTypes = cs.GetCardTypes(searchModel);
return View(searchModel);
}
and here is my view
@model CardSite.Models.CardTypeSearchModel
@{
ViewBag.Title = "Card types";
}
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Created new card type", "CreateCardType", "Card")</li>
</ul>
</div>
<div class="row">
<div class="col-md-4">
<h2>Card types</h2>
<p>
Here is the list of card types
</p>
</div>
</div>
@using (@Html.BeginForm("CardTypes", "Card", FormMethod.Get))
{
@Html.TextBoxFor(x => x.Name);
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(x => x.Name)
</th>
<th>
View
</th>
</tr>
@foreach (var item in Model.CardTypes)
{
<tr>
<td>
@Html.DisplayFor(m => item.Name)
</td>
<td>
<a href="/Card/CardType/@item.Id">View</a>
</td>
</tr>
}
</table>
If anyone has any ideas or tutorials that can help me that would be wonderful.
--- update ---
Just a quick update, I have moved my table cshtml code to a new view, this is the script i have at the moment
<script type="text/javascript">
$(document).ready(
$('#filter-card-types').click(function () {
var url = "Card/CardTypesTable/" + @Model.Name
$.get(url, function(data)
{
if(data)
{
$("div").find("#card-types-table").append(data);
}
})
})
);
</script>
now this is doing something but not the correct thing, it is basically finding my div which is correct, but the data is gets is the same page it is on, so it kinda re-renders itself inside of the new div. Any idea on why my data is the same as the view it is on?
--- update ---
Here is my new javascript function
$.get("@Url.Action("CardTypesTable", "Card")", { Name : '@Model.Name'}, function(data)
{
var div = $('div').find("#card-types-table");
div.empty();
alert(data);
$.each(data, function(items, item) {
div.append("Card type : " + item.Name);
});
})
this is working fine, but the problem I am having is the Name is never populated, even If i put something in my TextBox and click the filter button, any reasons why it isn't picking it up?