jsonresult

ASP.NET MVC - Pass Json String to View using ViewData

北战南征 提交于 2019-12-18 12:23:14
问题 I'm trying to pass Json to my View using ViewData Controller ViewData("JsonRegionList") = Json(RegionService.GetActiveRegions()) view $("input#UserRegion").autocomplete({ source:"<%: ViewData("JsonRegionList").ToString %>", minLength: 3, but the problem I'm running into is the output source looks like $("input#UserRegion").autocomplete({ source:"System.Web.Mvc.JsonResult", minLength: 3, which is obviously not right. Am I missing something basic? 回答1: The Json() controller method returns a

ASP.Net MVC: how to create a JsonResult based on raw Json Data

梦想与她 提交于 2019-12-18 04:09:05
问题 Having a string containing the following raw Json data (simplified for the sake of the question): var MyString = "{ 'val': 'apple' }"; How can I create a JsonResult object representing MyString ? I tried to use the Json(object) method. but it handles the raw json data as an string -logically :P-. So the returned HTTP response looks like: "{ 'val': 'apple' }" instead of the given raw Json Data: { 'val': 'apple' } this is what I want to achieve : 回答1: The Json() method on Controller is actually

How to unit test an Action method which returns JsonResult?

耗尽温柔 提交于 2019-12-17 22:35:05
问题 If I have a controller like this: [HttpPost] public JsonResult FindStuff(string query) { var results = _repo.GetStuff(query); var jsonResult = results.Select(x => new { id = x.Id, name = x.Foo, type = x.Bar }).ToList(); return Json(jsonResult); } Basically, I grab stuff from my repository, then project it into a List<T> of anonymous types. How can I unit-test it? System.Web.Mvc.JsonResult has a property called Data , but it's of type object , as we expected. So does that mean if I want to

Json structure is returned empty, without property names and values when using Newtonsoft JsonConvert in FB C# client

妖精的绣舞 提交于 2019-12-13 17:22:23
问题 I'm working on asp.net mvc 4 application which uses Facebook C# SDK (6.0.10.0) and Newtonsoft.Json (4.5.0.0). Request with FacebookClient returns expando object: [Authorize] public ActionResult GetFbData(string path = "me"){ var expando = this.fb.Get(path); return Json(expando); } Returned Json looks like: [{"Key":"id","Value":"100000xxxxxxxx"},{"Key":"name","Value":"John Doe"} ... ] I want to return it in format {id:100000xxxxxxx, name:"John Doe", ... } so I added this to the code which

How can i return Json Result + Web api + validate model + actionfilters +OnActionExecuting method

我与影子孤独终老i 提交于 2019-12-12 05:39:23
问题 string message = string.Empty; public override void OnActionExecuting(HttpActionContext actionContext) { var modelState = actionContext.ModelState; if (!modelState.IsValid) actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelState); foreach (var key in modelState.Keys) { var state = modelState[key]; if (state.Errors.Any()) { message = message + state.Errors.First().ErrorMessage; } } } Here i want to return message variable with Jsonresult, please

get json object with jquery

≯℡__Kan透↙ 提交于 2019-12-12 04:08:15
问题 $.getJSON("<%: Url.Action("myUrl", "cont") %>/", function(data) { var items = []; $.each(data, function(key, val) { items.push(val); }); }); [Authorize] [OutputCache(Duration = 0, VaryByParam = "None")] public JsonResult myUrl() { var list = _repository.GetAll(); var items = list.Select(c => c.Name).ToList(); return Json(items, JsonRequestBehavior.AllowGet); } I create a list on the server side (list of string names) and return a JsonResult. I'm trying to get the list on the client side using

How to handle youtube deleted videos using youtube api from json request

元气小坏坏 提交于 2019-12-11 23:16:30
问题 I want to get youtube videos from youtube api using json request. Now i get videos from youtube using json. Example the url have some deleted videos (http://www.youtube.com/watch?v=MlOHWLqgcoY&list=PLD62D6701B15FD3E1) but i get only playing videos not deleted videos. Is it possible or any other way to handle youtube deleted videos using json results. This is my code to get youtube videos string getPlaylistVideoUrl = https://gdata.youtube.com/feeds/api/playlists/PLD62D6701B15FD3E1?v=2

Simple JsonResult returns parsererror on Jquery ajax method

天涯浪子 提交于 2019-12-11 07:37:41
问题 i'm using asp.net mvc2 with jquery 1.5.2. what i pretend is to make client-side call to a specific method in my controller that returns a json data. Actually, my client-side call to server is working, but the problem is that jquery doesn't recognize the returned json. I cannot understand what i'm doing wrong!, can some one helpme with this? Controller method: <HttpPost()> _ Function DoStuff(ByVal id As Integer) As JsonResult Dim retval As JsonResult = Nothing retval = Json(New xpto With {.P1

Converting JsonResult into a different object in C#

爱⌒轻易说出口 提交于 2019-12-11 02:51:35
问题 So I have an object named Balance that contains: public class Balance { string balance1; string balance2; string currency; } and I'm trying to parse a JsonResult object that is returned by a different function call into an instance of Balance . I've tried using JsonConvert.Serialize and Deseralize<Balance> , however, the object that I'm trying to parse into is set to null every time (ie balance1 = null etc) Any help would be much appreciated. EDIT: Below is the code I'm trying to parse. Also,

Passing Json from action to view by ViewBag

送分小仙女□ 提交于 2019-12-08 12:33:22
问题 I'm trying to get the result below using JsonResult , but I can't var localJSON = [ { "id": "1", "label": "tagName1", "value": "tagValue1" }, { "id": "2", "label": "tagName2", "value": "tagValue2" }, { "id": "3", "label": "tagName3", "value": "tagValue3" }, { "id": "1553", "label": "tagName1553", "value": "tagValue1553" } ]; Here is the way I use: controller private JsonResult GetAvailableTags() { var tagsList = Facade.Tags.Get(CurrentLocale.ID); var retValue = new { id = tagsList.Select(x =>