如何从的ActionResult在控制器HTML返回 [英] How to get html returned from ActionResult in Controller
问题描述
我想实现类似相关问题上的计算器功能,我这样做是在MVC。
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();
}
}
这是我的控制器返回的数据是未定义,如图中的注释行中ajaxFinish。
我究竟做错了什么?
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);
}
编辑:
我想会帮助我学习和实施解决方案,以我塞纳里奥是,如果我能得到一些见解计算器如何实现此JavaScript方法:
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));
}
看起来像一个搜索文件夹中的文件夹视图和 PartialView 叫做标题。 A SearchController 的.cs用下面的方法:
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");
}
什么在Titles.ascx去显示HTML?
What goes in the Titles.ascx to display the html?
推荐答案
JSON()的目的是返回一个JSON对象 - 而不是HTML JSON对象会像{html_value: &LT;李&GT;嗒嗒}。我不知道你的Ajax请求期待。如果期待JSON(你没有数据类型设置两次),那么你可以这样做有一个匿名的对象:
The purpose of JSON() is to return a JSON object -- not HTML. JSON object would be something like {html_value: "<li>blah" }. I'm not sure what your ajax request is expecting. If it is expecting JSON (you have dataType set twice), then you can do something like with an anonymous object:
return Json(new {html_value = retVal});
不过,如果你想从你的控制器返回的 HTML - 不要。这是完全什么是视图是。创建无母版页的图,做循环和返回的HTML的方式。 Ajax应用程序可以把这个HTML并把它只要有必要的。
However, if you want to return HTML from your controller -- don't. That's exactly what a view is for. Create a view without any master page and do the loop and return the HTML that way. Ajax apps can take this HTML and drop it wherever necessary.
其实,当你在技术上可以做到上面匿名对象(如返回的HTML的JSON对象内),这是不是就是它的功能。如果你想使用JSON你应该返回值,并让客户端格式的JavaScript它:
In fact, while you technically could do the above anonymous object (where you return the html inside of a json object), this isn't what it's for. If you want to use JSON you should be returning values, and letting the javascript on the client format it:
我不知道你的问题是如何重的对象是的,但假设它只有你使用的三个字段。在这种情况下,做的:
I'm not sure how "heavy" your issues object is, but assume that it only has the three fields you're using. In that case, do:
return Json(issues);
修改
嗯,我认为最佳实践是给javascript内返回刚刚通过JSON的价值观和格式。我不和JSON()不够熟悉,但我知道它的工作原理(我使用的是简单的东西)。尝试创建一个只有这三个值的简单问题,对象和
Well, I think "Best Practice" would be to return just the values via JSON and format within the javascript. I'm not familiar enough with JSON(), but I know it works (I'm using it for something simple). Try creating a simple issues object with just those three values and
return Json(issuesTxfr);
您不需要使用partialviews因为你从一个控制器调用。只是把它当做一个非常简单的看法。下面是我的一个例子(请不要注意我没听懂我自己的JSON的建议 - 这是一段时间回来,我现在畏缩看它的几个原因):
You don't need to use partialviews as you're calling from a controller. Just think of it as a very simple view. Here's an example of mine (please don't notice that I'm not following my own JSON advice -- this is from a while back and I now cringe looking at it for a few reasons):
public ActionResult Controls_Search_Ajax(string q, string el)
{
...
ViewData["controls"] = controls;
ViewData["el"] = el;
return View();
}
和
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Controls_Search_Ajax.aspx.cs" Inherits="IRSoxCompliance.Views.Edit.Controls_Search_Ajax" %>
<% var controls = ViewData.Get<IEnumerable<IRSoxCompliance.Models.Control>>("controls");
var el = ViewData.Get<String>("el");
if (controls != null)
{
foreach (var c in controls)
{
%><%= c.Control_ID %>***<%= c.Full_Control_Name %>***<li id="<%= el %>:li:<%= c.Control_ID %>"><span class="item"><%= Html.BreadCrumb(c, false) %></span><span class="actions"><a href="#" onclick="sx_Remove_Control('<%= el %>', <%= c.Control_ID %>); return false;">Remove</a></span><br></li>
<% }
}
%>
请注意一个事实,即没有指定的母版页。
Note the fact that there is no master page specified.
这篇关于如何从的ActionResult在控制器HTML返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!