当我单击编辑按钮时。可编辑的数据将传递到相关的文本框中进行编辑。但没有显示数据。到目前为止我尝试过的东西附在下面。
Response.Write(JsonConvert.SerializeObject(employees));一个东西 非静态字段需要参考
这是一个按钮
{
"sTitle": "Edit",
"mData": "id",
"render": function (mData, type, row, meta) {
return '<button class="btn btn-xs btn-success"
onclick="get_category_details(' + mData + ')">Edit</button>';
}
如果我单击“编辑”按钮,则ID将成功传递给get_category_details方法。然后发布到edit_return.aspx / doSome页面 但未检索到数据。我认为出现错误Edit_retun.aspx。
function get_category_details(id) {
$.ajax({
type: 'POST',
url: 'edit_return.aspx/doSome',
dataType: 'JSON',
data: "{id: '" + id + "'}",
contentType: "application/json; charset=utf-8",
success: function (data) {
$("html, body").animate({ scrollTop: 0 }, "slow");
isNew = false
id = data.id
$('#fname').val(data.fname);
$('#age').val(data.age);
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
Edit_retun.aspx
public class Employee
{
public string id { get; set; }
public string fname { get; set; }
public string age { get; set; }
}
[WebMethod]
public static string doSome(int id)
{
SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
string sql = "select * from record where id='" + id + "'";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
List<Employee> employees = new List<Employee>();
employees = dt.AsEnumerable()
.Select(x => new Employee()
{
id = x.Field<int>("id").ToString(),
fname = x.Field<string>("name"),
age = x.Field<int>("age").ToString(),
}).ToList();
Response.Write(JsonConvert.SerializeObject(employees));
return JsonConvert.SerializeObject(employees);
}
HTML表单
<form id="frmProject" runat="server">
<div>
<label class="form-label">First Name</label>
<input type="text" id="fname" name="fname" class="form-control" required />
</div>
<div class="form-group" align="left">
<label class="form-label">Age</label>
<input type="text" id="age" name="age" class="form-control" required />
</div>
<div>
<input type="button" id="b1" value="add" class="form-control" onclick="addProject()" />
</div
</form>
答案 0 :(得分:1)
您不能在Response.Write
方法中使用static
。您的doSome
方法必须是non-static
方法。或者,您可以从您的方法中删除Response.write
,因为我猜这里不需要。
答案 1 :(得分:0)
[WebMethod]
public List<Employee> doSome(int id)
{
SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
string sql = "select * from record where id='" + id + "'";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
List<Employee> employees = new List<Employee>();
employees = dt.AsEnumerable()
.Select(x => new Employee()
{
id = x.Field<int>("id").ToString(),
fname = x.Field<string>("name"),
age = x.Field<int>("age").ToString(),
}).ToList();
return employees;
}
Your List<Employee> will be serialize automatically no need to serialize.
In ajax success
$.ajax({
success: function(data){
data=data.d;
// now data is JavaScript array of employee object
},
});