使用JsonResult

在MVC中使用JsonResult

controller 中定义以下方法:

public JsonResult UpdateSingle(int id, string actionName, string actionValue) 
  { 
   var res = new JsonResult(); 
   //var value = "actionValue"; 
   //db.ContextOptions.ProxyCreationEnabled = false; 
   var list = (from a in db.Articles 
      select new 
      { 
       name = a.ArtTitle, 
       yy = a.ArtPublishTime 
      }).Take(5); 
   //记得这里要select new 否则会报错:序列化类型 System.Data.Entity.DynamicProxies XXXXX 的对象时检测到循环引用。 
   //不select new 也行的加上这句 //db.ContextOptions.ProxyCreationEnabled = false; 
   res.Data = list;//返回列表 
 
   var name = "小华"; 
   var age = "12"; 
   var name1 = "小华"; 
   var age1 = "12"; 
   res.Data = new object[] { new { name, age }, new { name1, age1 } };//返回一个自定义的object数组 
 
   var person = new { Name = "小明", Age = 22, Sex = "男" }; 
   res.Data = person;//返回单个对象; 
 
   res.Data = "这是个字符串";//返回一个字符串,意义不大; 
 
   res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。 
   return res; 
  }

页面调用:

<a href="javascript:void(0);" onclick="javascript:upclick(this);">Click Me</a> 
<script type="text/javascript"> 
 function upclick(o) { 
  var obj = $(o); 
  alert(obj); 
  $.ajax({   
   url: "/Articles/UpdateSingle?ran=" + Math.random(), 
   type: "GET", 
   dataType: "json", 
   data: { id: obj.attr("id"), actionName: obj.attr("actionName"), actionValue: obj.attr("actionValue") }, 
   success: function (data) { 
//    if (data.result == "True") { 
//     alert("修改成功!"); 
//    } 
//    if (obj.attr("actionName") == "ArtVerify") { 
 
//    } 
    $(o).html(data[0].name); 
    obj.attr("actionValue", data[0].result); 
   } 
  }) 
 } 
</script> 

在WebForm中使用JsonResult

在webform中要引用Newtonsoft.Json.dll;

protected void Page_Load(object sender, EventArgs e) 
  { 
   var customer = new customer { name = "李华", sex = "男" }; 
   var customer1 = new customer { name = "小芳", sex = "女" }; 
   var li = new List<customer>(); 
   li.Add(customer); 
   li.Add(customer1); 
   var list = Newtonsoft.Json.JavaScriptConvert.SerializeObject(li); 
   var tt = "[{\"name\":\"李华\",\"sex\":\"男\"},{\"name\":\"小芳\",\"sex\":\"女\"}]"; 
   //new Newtonsoft.Json.JsonSerializer()..(customer); 
   Response.Write(tt); 
   Response.End(); 
  } 
 
  public class customer 
  { 
   public string name { get; set; } 
   public string sex { get; set; } 
  } 

页面方法:

<p> 
  <a href="javascript:void(0)" onclick="javascript:getJsonData();">GetJsonData</a> 
 </p> 
 <div id="dataDiv"> 
  ggg 
 </div> 
 <script type="text/javascript"> 
  function getJsonData() { 
   var str = ""; 
   $.getJSON("/Json.aspx", function (data) { 
    var tt = ""; 
    $.each(data, function (k, v) { 
     $.each(v, function (kk, vv) { 
      tt += kk + ":" + vv + "<br/>"; 
     }); 
    }); 
    $("#dataDiv").html(tt); 
   }); 
  } 
 </script> 

转载自:
http://www.manongjc.com/article/149006.html

已标记关键词 清除标记
相关推荐
<div class="post-text" itemprop="text"> <aside class="s-notice s-notice__info js-post-notice mb16" aria-hidden="false" role="status"> <div class="grid fd-column fw-nowrap"> <div class="grid fw-nowrap"> <div class="grid--cell fl1 lh-lg"> <div class="grid--cell fl1 lh-lg"> <b>Closed.</b> This question is <a href="/help/closed-questions">not reproducible or was caused by typos</a>. It is not currently accepting answers. </div> </div> </div> </div> <hr class="my12 outline-none baw0 bb bc-powder-2"> <div class="grid fw-nowrap fc-black-600"> <div class="grid--cell mr8"> <svg aria-hidden="true" class="svg-icon iconLightbulb" width="18" height="18" viewbox="0 0 18 18"><path d="M9.5.5a.5.5 0 0 0-1 0v.25a.5.5 0 0 0 1 0V.5zm5.6 2.1a.5.5 0 0 0-.7-.7l-.25.25a.5.5 0 0 0 .7.7l.25-.25zM1 7.5c0-.28.22-.5.5-.5H2a.5.5 0 0 1 0 1h-.5a.5.5 0 0 1-.5-.5zm14.5 0c0-.28.22-.5.5-.5h.5a.5.5 0 0 1 0 1H16a.5.5 0 0 1-.5-.5zM2.9 1.9c.2-.2.5-.2.7 0l.25.25a.5.5 0 1 1-.7.7L2.9 2.6a.5.5 0 0 1 0-.7z" fill-opacity=".4"></path><path opacity=".4" d="M7 16h4v1a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1v-1z" fill="#3F3F3F"></path><path d="M15 8a6 6 0 0 1-3.5 5.46V14a1 1 0 0 1-1 1h-3a1 1 0 0 1-1-1v-.54A6 6 0 1 1 15 8zm-4.15-3.85a.5.5 0 0 0-.7.7l2 2a.5.5 0 0 0 .7-.7l-2-2z" fill="#FFC166"></path></svg> </div> <div class="grid--cell lh-md"> <p class="mb0"> <b>Want to improve this question?</b> <a href="/posts/45724562/edit">Update the question</a> so it's <a href="/help/on-topic">on-topic</a> for Stack Overflow. </p> <p class="mb0 mt6">Closed <span title="2017-08-17 12:31:45Z" class="relativetime">2 years ago</span>.</p> </div> </div> </aside> <p>I'm having trouble writing this AJAX method.</p> <p>I want to return a <code>JsonResult</code>. (Seems like the logical thing to do.) All the examples I could find use <code>Json()</code> to convert the result to a <code>JsonResult</code>.</p> <pre><code>[HttpPost] public JsonResult GetScoreResults(string userId, int chapterId) { return new Json(ChapterScoreResultsModel.Create(DbContext, userId, chapterId)); } </code></pre> <p>But when I try this:</p> <blockquote> <p>The type or namespace name 'Json' could not be found (are you missing a using directive or an assembly reference?)</p> </blockquote> <p>However, I can't find this symbol anywhere and all the articles I found that used it didn't seem to do anything special. I did find the symbol in the namespace <code>System.Web.Helpers</code> but when I add a <code>using</code> for that, I get the error:</p> <blockquote> <p>Cannot create an instance of the static class 'Json'</p> </blockquote> <p>What is the <em>current</em> way to return a <code>JsonResult</code> from an AJAX method?</p> </div>
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页