jQuery 如何将 HTML 从 MVC 控制器返回到我视图中的 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10369936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I return the HTML from MVC controller to a div in my view
提问by Waas.CSpark
I'm trying to return the generated HTML string to the view to dynamically generate a HTML table with results. I'm not able to get the returned HTML string any suggestions and help is greatly appreciated.
我正在尝试将生成的 HTML 字符串返回到视图以动态生成带有结果的 HTML 表。我无法获得返回的 HTML 字符串任何建议和帮助非常感谢。
Here is my Controller code
这是我的控制器代码
public ActionResult ValidateTrams()
{
string html = "";
if (Request.Files.Count == 0 || Request.Files[0].ContentLength == 0)
{
}
else
{
html = ProcessTextFile(Request.Files[0].InputStream);
}
return View(html);
}
I'm trying to grab this returned result in jquery like this
我正在尝试像这样在 jquery 中获取这个返回的结果
$('#tramsView').live('click', function () {
$.ajax({
url: '/Booking/ValidateTrams',
type: 'POST',
dataType: 'jsonp',
success: function (data) {
alert(data);
$('#TramsViewFrame').html(data);
},
error: function (jqxhr, textStatus, errorThrown) {
$(window).hideWaitScreen();
if (confirm(errorThrown)) { window.location.reload(); }
}
});
});
});
Finally Below is the CSHTML for the form. Here I'm reading a file from a form with a button type submit
最后下面是表单的 CSHTML。在这里,我正在从带有按钮类型提交的表单中读取文件
<form action="#" method="post" enctype="multipart/form-data" class="forms" name="form"
id="frmvalidate">
<table>
<tr>
<td>
<input type='file' name='trams' id='ValidatetramsFile' />
</td>
</tr>
<tr>
<td>
<br />
<input name="cbDisplayUmatched" id="cbDisplayUmatched" type="checkbox" value="" checked="true" />
<label style="text-decoration: none; outline: none; font-size: 1.1em; padding: 3px 0 0px 0;">
Display rows that were <strong>NOT</strong> parsed</label>
</td>
</tr>
<tr>
<td>
<br />
<div class="buttons">
<button type="submit" value="VIEW" class="ui-state-default ui-corner-all" id="tramsView">VIEW</button>
</div>
</td>
</tr>
</table>
</form>
Thanks for your time and really appreciate your help. Kind Regards!!!
感谢您的时间,非常感谢您的帮助。亲切的问候!!!
采纳答案by ngm
It sounds as if your form is still submitting a normal postback, so any asynchronous calls that you're doing are being lost.
听起来好像您的表单仍在提交正常的回发,因此您正在执行的任何异步调用都将丢失。
Try preventing the default form submission taking place with the following:
尝试通过以下方式阻止默认表单提交:
$('#tramsView').live('click', function (evt) {
evt.preventDefault();
// ... rest of your code
});
Incidentally, in this case, if all you're doing is updating the html on your #TramsViewFrame
, you could just use the slightly simpler $.load()method:
顺便说一下,在这种情况下,如果您所做的只是更新 html 上的 html #TramsViewFrame
,则可以使用稍微简单的$.load()方法:
$('#tramsView').live('click', function (evt) {
evt.preventDefault();
$('#TramsViewFrame').load('/Booking/ValidateTrams');
});
回答by Jayantha Lal Sirisena
you can return HTML from action like this,
你可以从这样的动作中返回 HTML,
return Content(html, "text/xml");
回答by naveen
Make these changes
进行这些更改
Give the attribute HttpPost on top of your controller
在控制器顶部提供属性 HttpPost
[HttpPost]
public ActionResult ValidateTrams()
return the string as Json like this from the controller.
像这样从控制器返回字符串作为 Json。
return Json(new { success = html });
Finally change your dataType from to jsonp
json
最后将您的数据类型从到jsonp
json
$('#tramsView').live('click', function() {
$.ajax({
url: '/Booking/ValidateTrams',
type: 'POST',
dataType: 'json',
success: function(data) {
alert(data.success);
$('#TramsViewFrame').html(data.success);
},
error: function(jqxhr, textStatus, errorThrown) {
$(window).hideWaitScreen();
if (confirm(errorThrown)) {
window.location.reload();
}
}
});
});?
P.S: If you are using the latest version of jQuery (1.7+), please change the .live
handler to .on
handler. Not mandatory :)
PS:如果您使用的是最新版本的jQuery(1.7+),请将.live
处理.on
程序更改为处理程序。不是强制性的 :)
回答by COLD TOLD
In your controller since you are using ajax post you need to return as JSON it would be something like this
在您的控制器中,因为您使用的是 ajax 帖子,所以您需要以 JSON 的形式返回它会是这样的
return Json(html);
回答by maztt
i think this is another way relevant to your scenario How can I return the HTML from MVC controller to a div in my view
http://mazharkaunain.blogspot.com/2011/02/how-to-use-aspnet-mvc-javascriptresult.html
我认为这是与您的场景相关的另一种方式How can I return the HTML from MVC controller to a div in my view
http://mazharkaunain.blogspot.com/2011/02/how-to-use-aspnet-mvc-javascriptresult.html
回答by code black
instead of storing in "string" store html codes in following format:
而不是存储在“字符串”存储以下格式的html代码:
HtmlString s = new HtmlString("<html coding/>");
ViewData["Id_as_per_your_choice"] = s;
return View("page_you_want to_respond");
then place "<%: ViewData["Id_Same_as_that_in_above_code"] %>" where ever you want that html code to appear......simple..... but make initial value of Viewdata as null in "index()"method so that code doesnot appear on page load....
然后将 "<%: ViewData["Id_Same_as_that_in_above_code"] %>" 放在你希望 html 代码出现的地方......简单......但在 "index()" 中将 Viewdata 的初始值设为 null方法,以便代码不会出现在页面加载....