i have some trouble while making two dropdownlist:
Area: All Areas
City: City that belong to the selected value of the Area
everything is fine till City read the items from JSON. All of the items in dropdown City are Undefined.
Here's my code
$(document).ready(function () {
$('#AreaList').change(function () {
$.ajaxSetup({ cache: false });
var selectedItem = $(this).val();
if (selectedItem == "" || selectedItem == 0) {
//Do nothing
} else {
$.ajax({
url: '<%=Url.Content("~/") %>Administration/GetDropDownCity',
data: { item: $("#AreaList> option:selected").attr("value") },
dataType: 'json',
traditional: true,
type: 'POST',
success: function (data) {
var items = "";
$.each(data, function (i, data) {
items += "<option value='" + data.value + "'>" + data.description + "</option>";
});
$("#CityList").html(items);
$("#CityList").removeAttr('disabled');
}
});
}
});
});
The Json does exist, I check on the Firebug and listed as below:
"[{\"value\":\"107\",\"description\":\"KOTA DEPOK\"},{\"value\":\"141\",\"description\":\"KOTA JAKARTA SELATAN\"}]"
any idea to solve this problem?
Edit
The problem is solved: I use jQuery.parseJSON() to read JSON.
I'm not sure but I think a variable is in conflict here
$.each(data, function (i, data) {
items += "<option value='" + data.value + "'>" + data.description + "</option>";
});
in enumerator change data to val for instance like
$.each(data, function (i, val) {
items += "<option value='" + val.value + "'>" + val.description + "</option>";
});
Edit: also try alerting data[0].value or data[0].description to make sure that json string is parsed correctly on client side, otherwise you may have to call $.parseJson on returned data.
Edit 2: one thing you can try is to use for loop
for(i=0;i<data.length;i++)
{
items += "<option value='" + data[i].value + "'>" + data[i].description + "</option>";
}
Related
Can anybody tell me where I am wrong. I used this sample code in my other project, it's working fine but in my current project when I use this, it's just fetching first values from array but I want to get all the list values from array! So I hope you understand what I am saying. If you need to ask something more about this please comment.
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("#btn").click(function () {
var degree = $(".txtcourse").val();
var institute = $(".txtinstitute").val();
var title = $(".txttitle").val();
var from = $(".fromdate").val();
var to = $(".todate").val();
$("tbody").append("<tr>" + "<td>" + degree + "</td>" + "<td>" + institute + "</td>" + "<td>" + title + "</td>" + "<td>" + from + "</td>" + "<td>" + to + "</td>" + + "<td>" + "<td><button name='record' type='button'><i class='fa fa-trash fa-sm'></i></button></td>" + "</tr>");
});
//$("button").click(function () {
// $("tbody").find('input[txtcourse="record"]').each(function () {
// $(this).parents("tr").remove();
// });
//});
$("#eduadd").on("click", function () {
debugger;
var txtcourse = Array();
var txtinstitute = Array();
var txttitle = Array();
var fromdate = Array();
var todate = Array();
$(".txtcourse").each(function (i, v) {
txtcourse.push($(this).val());
});
$(".txtinstitute").each(function (i, v) {
txtinstitute.push($(this).val());
});
$(".txttitle").each(function (i, v) {
txttitle.push($(this).val());
});
$(".fromdate").each(function (i, v) {
fromdate.push($(this).val());
});
$(".todate").each(function (i, v) {
todate.push($(this).val());
});
var postData = {
course: txtcourse, institute: txtinstitute, title: txttitle, frmdate: fromdate, tdate: todate
};
$.ajax({
type: "POST",
url: "/GuardEduaction/SaveEducation",
data: postData,
success: function (data) {
alert("saved");
},
error: function (data) {
alert("Error");
},
dataType: "json",
traditional: true });
});
});
</script>
I am trying to bind data into grid view using jquery. data is succesfully loaded in "result" but can not apply this data in grid view
My Script:
$(document).ready(function() {
$.ajax({
url: '../_AJAX/ajaxCall-InterestSubsidy.aspx',
data: { 'MODE': 'BindGrid' },
type: 'POST',
dataType: 'json',
success: function(result) {
var reslk = result;
$.each(result.Table1, function(index, res) {
$(".GridView1").append("<table><tr><td>" + res.StateId +
"</td><td>" + res.StateName +
"</td></tr></table>");
});
},
error: function(e) {
}
});
});
My grid view:
<asp:GridView ID="GridView1" runat="server" CssClass="GridView1">
</asp:GridView>
try this
$.map(result.d, function (item) {
$(".GridView1").append("<tr><td>" + item.StateId +
"</td><td>" + item.StateName +
"</td></tr>");
});
or
$.map(result.Table1, function (index, res) {
$(".GridView1").append("<tr><td>" + res.StateId +
"</td><td>" + res.StateName +
"</td></tr>");
});
I would like to add Items in jcarousel using c# Webmethod + jquery Ajax
for that i made something like this :
my Html is like :
<div>
<ul id="mycarousel" class="jcarousel-skin-tango" style="float: left">
</ul>
</div>
Jquery code for jcarousel and Ajax Method is like this :
$("#mycarousel").empty();
var element =jQuery('#mycarousel');
$.ajax({
url: "Home.aspx/GetProjectData",
type: "POST",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: "{}",
async: false,
success: function (response) {
if (response.d != null) {
//$.each(response.d, function (i, response) {
$("#mycarousel").html('response.d');
element.jcarousel(
{
pager: true,
visible: 6
});
}
else {
}
},
error: function (xhr) {
}
});
and webmethod is like this :
[WebMethod]
public static List<string> GetProjectData()
{
// here i have 3 list in returnvalue
foreach (var item in returnvalue)
{
var classvalue = item.Soid + "|"
+ item.ProjectTitle + "|"
+ item.Role + "|"
+ item.StartDate + "|"
+ item.EndDate + "|"
+ item.Location.Country + "|"
+ item.Location.State + "|"
+ item.Location.City + "|";
string Template = "<li><img src='../Images/DefaultPhotoMale.png' class='"+ classvalue + "' width='40' height='40' alt='image'/></li>";
list.Add(Template);
}
return list;
}
but problem is , i am not able to images in jcarousel , i only see white box, i am not able to seee images inside, why ?
I'm not sure but wouldn't you need to append the elements sort of like this:
var listItem = $(response.d); //I'm guessing reponse.d is your returned li
element.append(listItem);
I have a JSON list of SelectListItems that I grab:
new SelectListItem
{
Text = item.Name,
Value = item.Id.ToString(),
Selected = item.Id.Equals(userId)
}).ToList();
Then this list is connected to a dropdown menu with JQuery:
$.Ajax call for an object above goes here...
function (data) {
data= $.map(data, function (item, a) {
return "<option value=" + item.Value + ">" + item.Text + "</option>";
});
$("#edit-user-list").html(data.join(""));
This works and populates my list just fine.
I can't figure out syntax how to indicate my SELECTED item though!
Can you guys help me out ?
How with this JQuery can I indicate the selected item that's indicated in the passed in JSON collection of items?
Thank you
function (data) {
data= $.map(data, function (item, a) {
return "<option value=" + item.Value + " " + (item.Selected ? "selected": "") + ">" + item.Text + "</option>";
});
$("#edit-user-list").html(data.join(""));
I am using ASPNET MVC 2.0. I'm trying to pass a value from View to Controller function using jquery function .ajax. My jquery function is:
$(document).ready(function() {
$("#Search").click(function(event) {
var searchString = $("#TraderSearch").val();
$.ajax({
type: 'POST',
url: '/Build/SearchTrader',
data: "{strData : '" + searchString + "' }",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(ResultList) {
var contents = "";
var count = 0;
$(ResultList).each(function() {
contents = contents + '<tr><td>' + ResultList[count].Name + '</td><td>' + ResultList[count].Value +
'</td><td><a class="edit"><img src="../../html/images/Edit.gif" width="14" height="14" alt="edit" /></a></td></tr>';
count = count + 1;
});
$("#SerachResultList").append(contents);
alert("{strData : '" + searchString + "' }");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + textStatus + "\n" + errorThrown);
}
});
});
});
And my Controller function is as follows:
public ActionResult SearchTrader(string strData)
{
//Function to search DB based on the string passed
return Json(lstDictObject);
}
My problem is that, I am not able to get the value in my controller. I'm getting strData as 'null'. I think there is somme mistake in the way i'm trying to pass the value? Can anyone correct me?
Thanks in Advance,
Vipin Menon
try this:
data: "strData = " + searchstring
I believe the following should work:
data: { strData: searchstring }
Quotes caused the same issue in the script I just tested.
You also need to check the spelling on your "#SerachResultList" if this is your actual code.