this is my code ajax
$("#btnSearch").click(function () {
var goverId = parseInt($("#SellerSearch_gover_id").val());
var cityId = parseInt($("#SellerSearch_city_id").val()) || 0;
var setData = $("#tableid");
setData.html("");
$.ajax({
type: "POST",
url: "/ReSeller/GetSearch?goverId" + goverId + "&cityId" + cityId,
contentType: "html",
success: function (result) {
if (result.length == 0) {
setData.append('<tr style="color: red; "><td colspan="3">No match data</td></tr>')
}
else {
$.each(result, function (index, value) {
var Data = "<tr>" +
"<td>" + value.seller.seller_name_en + "</td>" +
"<td>" + value.seller.governorate.gover_name_en + "</td>" +
"<td>" + value.seller.city.city_name_en + "</td>" +
"<td>" + value.seller.address_en + "</td>" +
"<td>" + value.seller.phone1 + "</td>" +
"<td>" + value.seller.phone2 + "</td>" +
"</tr>";
setData.append(Data);
})
}
},
error: function (xhr, ajaxOptions, errorThrown) {
alert(xhr.responseText);
}
})
});
and this is my C# Code:
public JsonResult GetSearch(int goverId, int cityId)
{
SellerSearchViewModel model = new SellerSearchViewModel();
try
{
var result = db.seller.Where(x => x.gover_id == goverId || x.city_id == cityId).ToList();
model.seller = result;
}
catch(FormatException)
{
Console.WriteLine("Error");
}
return Json(model, JsonRequestBehavior.AllowGet);
}
and this html :-
@model WepProjectWhiteWhale.Models.SellerSearchViewModel
@{
ViewBag.Title = WhiteWhaleLanguage.ReSeller;
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
table {
table-layout: fixed;
word-wrap: break-word;
}
.breadcrumb > li + li:before {
color: #646464;
}
.Disable_ActiveClear {
pointer-events: none;
cursor: default;
}
</style>
<div class="breadcrumbs">
<div class="container">
<ol class="breadcrumb breadcrumb1 animated wow slideInLeft" data-wow-delay=".5s" style="background-color: #b1b1b2;">
<li><a href="@Url.Action("Index", "Home" )"><span class="glyphicon glyphicon-home" aria-hidden="true"></span>@WhiteWhaleLanguage.Home</a></li>
<li class="active" style="color: #3f3f3f;">@WhiteWhaleLanguage.ReSeller</li>
</ol>
</div>
</div>
<div class="checkout">
<div class="container" style="margin-bottom: 20px;">
@*@using (Html.BeginForm("Index", "ReSeller", FormMethod.Post))
{*@
@if (UICulture == "ar")
{
@Html.DropDownListFor(model => model.SellerSearch.gover_id, (SelectList)ViewBag.GovernorateAr,
WhiteWhaleLanguage.PleaseSelectYourGovernorate, new { @style = "padding:0 12px; margin-bottom: 15px; margin-left: 10px; width:40%;", @class = "form-control", required = "required", title = @WhiteWhaleLanguage.Required })
@Html.ValidationMessageFor(model => model.SellerSearch.gover_id, "", new { @class = "text-danger" })
}
else
{
@Html.DropDownListFor(model => model.SellerSearch.gover_id, (SelectList)ViewBag.GovernorateEn,
WhiteWhaleLanguage.PleaseSelectYourGovernorate, new { @style = "padding:0 12px; margin-bootom: 15px; margin-left: 10px; width:40%", @class = "form-control", required = "required", title = @WhiteWhaleLanguage.Required })
@Html.ValidationMessageFor(model => model.SellerSearch.gover_id, "", new { @class = "text-danger" })
}
@if (UICulture == "ar")
{
@Html.DropDownListFor(model => model.SellerSearch.city_id, (SelectList)ViewBag.CityAr,
WhiteWhaleLanguage.PleaseSelectYourCity, new { @style = "padding:0 12px; margin-bottom: 15px; margin-left: 10px; width:40%", @class = "form-control" })
}
else
{
@Html.DropDownListFor(model => model.SellerSearch.city_id, (SelectList)ViewBag.CityEn,
WhiteWhaleLanguage.PleaseSelectYourCity, new { @style = "padding:0 12px; margin-bottom: 15px; margin-left: 10px; width:40%;", @class = "form-control" })
}
<input type="submit" id="btnSearch" name="" value=@WhiteWhaleLanguage.Search , class="btn btn-primary" style="width:180px;" />
@* } *@
</div>
<div class="container">
<div class="checkout-right animated wow slideInUp" data-wow-delay=".5s">
<table class="timetable_sub">
<thead>
<tr>
<th></th>
<th>
@WhiteWhaleLanguage.ReSellerName
</th>
<th>
@WhiteWhaleLanguage.gover_id
</th>
<th>
@WhiteWhaleLanguage.city_id
</th>
<th>
@WhiteWhaleLanguage.Address
</th>
<th>
@WhiteWhaleLanguage.phone1
</th>
<th>
@WhiteWhaleLanguage.phone2
</th>
</tr>
</thead>
<tbody id="tableid">
@{ var i = 0; }
@foreach (var item in Model.seller)
{
<tr class="rem1">
<td class="invert">@(i += 1)</td>
<td class="invert">
@if (UICulture == "ar")
{
@Html.DisplayFor(modelItem => item.seller_name_ar)
}
else
{
@Html.DisplayFor(modelItem => item.seller_name_en)
}
</td>
<td class="invert">
@if (UICulture == "ar")
{
@Html.DisplayFor(modelItem => item.governorate.gover_name_ar)
}
else
{
@Html.DisplayFor(modelItem => item.governorate.gover_name_en)
}
</td>
<td class="invert">
@if (UICulture == "ar")
{
@Html.DisplayFor(modelItem => item.city.city_name_ar)
}
else
{
@Html.DisplayFor(modelItem => item.city.city_name_en)
}
</td>
<td class="invert">
@if (UICulture == "ar")
{
@Html.DisplayFor(modelItem => item.address_ar)
}
else
{
@Html.DisplayFor(modelItem => item.address_en)
}
</td>
<td class="invert">
@Html.DisplayFor(modelItem => item.phone1)
</td>
<td class="invert">
@Html.DisplayFor(modelItem => item.phone2)
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
What I have tried:
i want create search using ajax