Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get html returned from ActionResult in Controller
    text
    copied!<p>I am trying to implement a feature similar to the Related Questions on StackOverflow, I am doing this in MVC.</p> <pre><code>$().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(); } } </code></pre> <p>The data being returned from my controller is 'undefined', as shown by the commented line in ajaxFinish.<br> <strong>What am I doing wrong?</strong></p> <pre><code>//[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 += "&lt;div class='answer-summary' style='width: 610px;'&gt;"; retVal += "&lt;a href='Issue.aspx?projid=" + issue.ProjectId.ToString() + "&amp;issuetypeid=" + issue.IssueTypeId.ToString() + "&amp;issueid=" + issue.IssueId.ToString() + "'&gt;"; retVal += issue.Summary; retVal += "&lt;/a&gt;"; retVal += "&lt;/div&gt;"; } return Json(retVal); } </code></pre> <p>EDIT:</p> <p>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:</p> <pre><code>function QuestionSuggestions() { var s = $("#title").val(); if (s.length &gt; 2) { document.title = s + " - Stack Overflow"; $("#question-suggestions").load("/search/titles?like=" + escape(s)); } </code></pre> <p>Looks like a '<strong>Search</strong>' folder in the Views folder and a <strong>PartialView</strong> called '<strong>Title</strong>'. A <strong>SearchController</strong>.cs with the following method:</p> <pre><code>public ActionResult titles(string like) { // HOW TO IMPLEMENT THIS return PartialView("Titles"); } </code></pre> <p>What goes in the Titles.ascx to display the html?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload