i have this drag and drop datalist and two textbox.
, when i click a button, it should be able to call my rest service via jquery.ajax. to post the data into my database.
okay my question is
1.how can i pass in multiple value when i dropped? (eg. when i dropped, i should be able to retrieve the id , name and category of the item)
2. i'm not sure if my webInvoke POST is working, cos when i test it out, it states that the method is not working / endpoint not found. However i am able to create/post through my wcf test cient.
Pls help me up, Thanks alot!!
my jquery
$("#Button1").click(function () {
var data1 = new Array();
});
$.ajax({
type: 'POST',
url: 'http://localhost:1990/Service1.svc/InsertPlan?PlanDate={0}&Category={0}&FoodName={0}&TCalories={0}&FoodFk={0}&EmailFK={0}',
contentType: "application/json; charset=utf-8",
data: '{ food:[' + 'calories:[' + 'name:[' +
data.join() + ']]]}',
dataType: 'json',
success: function (results) { alert(results.d); },
error: function () { alert('error'); }
});
});
$("div .block").each(function () {
this.addEventListener('dragstart', OnDragStart, false);
});
$("div .drop").each(function () {
this.addEventListener('dragenter', OnDragEnter, false);
this.addEventListener('dragleave', OnDragLeave, false);
this.addEventListener('dragover', OnDragOver, false);
this.addEventListener('drop', OnDrop, false);
this.addEventListener('dragend', OnDragEnd, false);
});
});
//on drop
function OnDrop(e) {
if (e.preventDefault) {
e.preventDefault();
}
srcElement.style.opacity = '1';
$(this).removeClass('highlight');
var count = $(this).find("div[data-food-name='" + e.dataTransfer.getData('text/html') + "']").length;
$(this).append("<div class='selectedfood' data-food-name='" + e.dataTransfer.getData('text/html') + "'>" + e.dataTransfer.getData('text/html') + "</div>");
alert(data)
}
function OnDragEnd(e) {
$("div .drop").removeClass('highlight');
this.style.opacity = '1';
}
my service.svc
public bool InsertPlan(string Date, string Category, string food, string calories, string foodfk , string email)
{
string connstring = ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
string queryStr = "INSERT INTO [Plans] (PlanDate, Category, FoodName,TCalories, FoodFk, EmailFK)"
+ "values (@date, @cat, @food, @cal , @foodfk, @email)";
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(queryStr, conn);
cmd.Parameters.AddWithValue("@date", Date);
cmd.Parameters.AddWithValue("@cat", Category);
cmd.Parameters.AddWithValue("@food", food);
cmd.Parameters.AddWithValue("@cal", calories);
cmd.Parameters.AddWithValue("@foodfk", foodfk);
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0){
return true;
}
else{
}
return false;
}
//my iservice.cs
[WebInvoke(Method="POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "InsertPlan?PlanDate={Date}&Category={Category}&FoodName={food}&TCalories={calories}&FoodFk={foodfk}&EmailFK={email}")]