I am trying to implement a feature similar to the Related Questions on StackOverflow, I am doing this in MVC.
$().ready(function() {
var s = $("#Summary").val();
$("#Summary").blur(function() { QuestionSuggestions(s); });
});
function GetPastIssues(title) {
$(document).ready(function() {
$.ajax({ type: "POST",
url: "/Issue/GetSimilarIssues",
contentType: "application/json; charset=utf-8",
dataType: "xml",
dataType: "json",
data: "{'title':'" + title + "'}",
processData: false,
error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
success: function(xml) { ajaxFinish(xml); }
});
});
function ajaxFinish(xml) {
if (xml.d != "NO DATA") {
$('#question-suggestions').html(xml.d); //alert(xml.d); // This ALERT IS returning undefined
$('#question-suggestions').show();
}
}
The data being returned from my controller is 'undefined', as shown by the commented line in ajaxFinish.
What am I doing wrong?
//[AcceptVerbs(HttpVerbs.Get)]
[JsonParamFilter(Param = "title", TargetType = typeof(string))]
public ActionResult GetSimilarIssues(string title)
{
var issues = _db.GetSimilarIssues(title).ToList();
if (title == null || issues.Count() == 0)
return Json("NO DATA");
string retVal = null;
foreach (Issue issue in _db.GetSimilarIssues(title))
{
retVal += "<div class='answer-summary' style='width: 610px;'>";
retVal += "<a href='Issue.aspx?projid=" + issue.ProjectId.ToString() + "&issuetypeid=" + issue.IssueTypeId.ToString() +
"&issueid=" + issue.IssueId.ToString() + "'>";
retVal += issue.Summary;
retVal += "</a>";
retVal += "</div>";
}
return Json(retVal);
}
EDIT:
I think what will help me learn and implement a solution to my senario is if I can get some insight into how StackOverflow implements this javascript method:
function QuestionSuggestions() {
var s = $("#title").val();
if (s.length > 2) {
document.title = s + " - Stack Overflow";
$("#question-suggestions").load("/search/titles?like=" + escape(s));
}
Looks like a 'Search' folder in the Views folder and a PartialView called 'Title'. A SearchController.cs with the following method:
public ActionResult titles(string like)
{
// HOW TO IMPLEMENT THIS
return PartialView("Titles");
}
What goes in the Titles.ascx to display the html?