How to set static text in JsonResult?

I found the following code example (from Telerik ) that I am trying to understand. I need to somehow set the static text in JsonResult (for example, Text = "Abc" and Value = "123")

public ActionResult _AjaxLoading(string text) { Thread.Sleep(1000); using ( var nw = new NorthwindDataContext() ) { var products = nw.Products.AsQueryable(); if ( text.HasValue() ) { products = products.Where((p) => p.ProductName.StartsWith(text)); } return new JsonResult { Data = new SelectList(products.ToList(), "ProductID", "ProductName") }; } } 
+2
source share
3 answers

This is what you are looking for

 return new JsonResult { Text = "Abc", Value="123" }; 

If you want to add a new item to the drop-down list at startup,

 var editedProducts = new SelectList(products.ToList(), "ProductID","ProductName" ).ToList(); editedProducts.insert(0, new SelectListItem() { Value = "123", Text = "Abc" }); return new JsonResult { Data = editedProducts }; 
+1
source
 public ActionResult _AjaxLoading(string text { var data = new { Text= "123", Value= "Abc"}; return Json(data, JsonRequestBehavior.AllowGet); } 

If it is an HTTPGet method, you must specify JsonRequestBehavior.AllowGet as the second parameter to return JSon data from the GET method

+3
source

It looks like you need this:

 return new JsonResult { Data = new { Text="Abc", Value="123", Produtcs= new SelectList(products.ToList(), "ProductID", "ProductName") }}; 
+2
source

Source: https://habr.com/ru/post/1411336/


All Articles