我正在开发一个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