我正在開發一個asp.net MVC web應用程序,其中我使用Linq to Sql通過jquery和ajax從數據庫獲取結果。我的模型下面的代碼Linq to SQL獲取多列
public IEnumerable<string> getComments(long problemID)
{
var comment = from c in _objectModel.Comments
where c.ProblemID == problemID
select new { c.EmpID, c.CommentText, c.Time }.ToString();
return comment;
}
和我的控制器有以下代碼
public string GetComments(string problemid)
{
List<string> collection = _discussionRepository.getComments(Convert.ToInt32(problemid)).ToList();
string comments = null;
foreach (string item in collection)
{
comments += item + "\n";
}
return comments;
}
在我看來,包含
$("#btnPostComment").click(function() {
var strdata = {
problemID: $("#problemID").val(),
commentText: $("#_1").val(),
empID: $("#empID").val(),
agree: 0,
disagree: 0
};
$.ajax({
type: "POST",
url: "<%= Url.Action("PostComment", "Discussion") %>",
data: strdata,
error: function(msg){
alert("error" + msg);
},
success: function (msg) {
var id = { problemid : $("#problemID").val()};
$.ajax({
type: "GET",
url: "<%= Url.Action("GetComments", "Discussion") %>",
data: id,
error: function(msg){
alert("error2" + msg);
},
success: function (msg) {
$("#commentdiv").html(msg);
}
});
}
});
,我得到下面的結果在我的ASP頁面
{EmpID = 1,CommentText = sss,Time = 2012年1月27日上午2:20:49} {EmpID = 1, CommentText = aaa,Time = 2012年1月27日上午2:46:07 {EmpID = 1,CommentText = aaa,Time = 2012年1月27日上午2:50:23} {EmpID = 1,CommentText = Munazza,時間= 2012年1月27日上午2:58:29} {EmpID = 1,CommentText = Jawad,時間= 2012年1月27日3:00:51} {EmpID = 1,CommentText = xx,時間= 1/28/2012 11:56:59 AM} {EmpID = 1,CommentText = ss,時間= 2012年1月28日12:35:00}
我想得到沒有大括號和沒有屬性的結果,即1 SS 2012年1月27日
問候
請告訴我發送字符串到視圖的控制器的代碼 – Snake 2012-01-28 08:07:18
@Snake:通過,我害怕 - 我還沒有做任何ASP.NET MVC。據推測,你並不直接在視圖中查看它,而是通過AJAX調用使其可用。感覺就像你現在可能略微超過你的頭 - 這些技術中有多少對你來說是新的?如果我只嘗試一次學習*一件新事物,我發現它效果最好。 – 2012-01-28 08:12:40