我试图创建控制器动作,根据参数返回JSON或部分html。异步将结果返回到MVC页面的最佳方法是什么?
当前回答
与编码框架的替代解决方案
操作返回json
控制器
[HttpGet]
public ActionResult SomeActionMethod()
{
return IncJson(new SomeVm(){Id = 1,Name ="Inc"});
}
剃须刀页面
@using (var template = Html.Incoding().ScriptTemplate<SomeVm>("tmplId"))
{
using (var each = template.ForEach())
{
<span> Id: @each.For(r=>r.Id) Name: @each.For(r=>r.Name)</span>
}
}
@(Html.When(JqueryBind.InitIncoding)
.Do()
.AjaxGet(Url.Action("SomeActionMethod","SomeContoller"))
.OnSuccess(dsl => dsl.Self().Core()
.Insert
.WithTemplate(Selector.Jquery.Id("tmplId"))
.Html())
.AsHtmlAttributes()
.ToDiv())
操作返回html
控制器
[HttpGet]
public ActionResult SomeActionMethod()
{
return IncView();
}
剃须刀页面
@(Html.When(JqueryBind.InitIncoding)
.Do()
.AjaxGet(Url.Action("SomeActionMethod","SomeContoller"))
.OnSuccess(dsl => dsl.Self().Core().Insert.Html())
.AsHtmlAttributes()
.ToDiv())
其他回答
与编码框架的替代解决方案
操作返回json
控制器
[HttpGet]
public ActionResult SomeActionMethod()
{
return IncJson(new SomeVm(){Id = 1,Name ="Inc"});
}
剃须刀页面
@using (var template = Html.Incoding().ScriptTemplate<SomeVm>("tmplId"))
{
using (var each = template.ForEach())
{
<span> Id: @each.For(r=>r.Id) Name: @each.For(r=>r.Name)</span>
}
}
@(Html.When(JqueryBind.InitIncoding)
.Do()
.AjaxGet(Url.Action("SomeActionMethod","SomeContoller"))
.OnSuccess(dsl => dsl.Self().Core()
.Insert
.WithTemplate(Selector.Jquery.Id("tmplId"))
.Html())
.AsHtmlAttributes()
.ToDiv())
操作返回html
控制器
[HttpGet]
public ActionResult SomeActionMethod()
{
return IncView();
}
剃须刀页面
@(Html.When(JqueryBind.InitIncoding)
.Do()
.AjaxGet(Url.Action("SomeActionMethod","SomeContoller"))
.OnSuccess(dsl => dsl.Self().Core().Insert.Html())
.AsHtmlAttributes()
.ToDiv())
public ActionResult GetExcelColumn()
{
List<string> lstAppendColumn = new List<string>();
lstAppendColumn.Add("First");
lstAppendColumn.Add("Second");
lstAppendColumn.Add("Third");
return Json(new { lstAppendColumn = lstAppendColumn, Status = "Success" }, JsonRequestBehavior.AllowGet);
}
}
在action方法中,返回Json(object)将Json返回到页面。
public ActionResult SomeActionMethod() {
return Json(new {foo="bar", baz="Blech"});
}
然后使用Ajax调用action方法。您可以使用ViewPage中的一个辅助方法,例如
<%= Ajax.ActionLink("SomeActionMethod", new AjaxOptions {OnSuccess="somemethod"}) %>
SomeMethod将是一个javascript方法,然后计算返回的Json对象。
如果你想返回一个普通的字符串,你可以使用ContentResult:
public ActionResult SomeActionMethod() {
return Content("hello world!");
}
ContentResult默认返回一个文本/纯文本作为它的contentType。 这是可重载的,所以你还可以做:
return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");
另一种处理JSON数据的好方法是使用JQuery getJSON函数。你可以致电
public ActionResult SomeActionMethod(int id)
{
return Json(new {foo="bar", baz="Blech"});
}
方法从jquery getJSON方法简单…
$.getJSON("../SomeActionMethod", { id: someId },
function(data) {
alert(data.foo);
alert(data.baz);
}
);
要回答问题的另一半,你可以拨打:
return PartialView("viewname");
当你想返回部分HTML时。您只需要找到某种方法来决定请求是想要JSON还是HTML,可能是基于URL部分/参数。
推荐文章
- HttpOnly cookie如何与AJAX请求一起工作?
- 如何合并2 JSON对象从2个文件使用jq?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 如何在Kotlin解析JSON ?
- JSON文件的蒙古导入
- 如何用jQuery / JavaScript解析JSON数据?
- jQuery在请求体中发布有效的json
- 将JSON显示为HTML
- 缓存一个HTTP 'Get'服务响应在AngularJS?
- 在JSON键名中哪些字符是有效的/无效的?
- 如何JSON序列化集?
- 在ASP .NET身份声明是什么
- 如何阻止恶意代码欺骗“Origin”报头来利用CORS?
- 如何检查JSON键是否存在?