将c#数据传递给javascript并分解为可用格式的最佳方法 [英] Best way to pass c# data to javascript and break apart into a useable format

查看:131
本文介绍了将c#数据传递给javascript并分解为可用格式的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,经过大量的谷歌搜索后,我正在努力寻找最佳方法,而不使用第三方库。我很乐意为此提供一些建议,因为它的关键是让我的程序正确。



我的程序是带有Html / javascript前端的C#webforms应用程序。 C#端在Sql压缩数据库上运行Sql查询,意味着将数据发送到javascript以获取部分并一次加载1行。



我有一个商店表,其中包含ID,名称,地址和类别字段。

 DataSet ds =  new  DataSet(); 
adapter.Fill(ds);

List< Shop> shops = new List< Shop>();

foreach (DataRow row in ds.Tables [ 0 ]。行)
{

string ID = row [ ShopID]。ToString();
string 名称=行[ ShopName ]的ToString();
string 地址=行[ ShopAddress ]的ToString();
string 类别=行[ 类别 ]的ToString();

shops.Add( new 商店(ID,名称,地址,类别));

}

JavaScriptSerializer ser = new JavaScriptSerializer();
hfData.Value = ser.Serialize(商店);





此部分似乎工作正常,数据正确加载并放入名为hfData的隐藏字段。



虽然在前端我有这条线应该读隐藏的区域



  var  HiddenValue =  document  .getElementById( <%= hfData.Value%>)。value; 
alert(HiddenValue);







HiddenValue警报以此格式返回。

[{ShopID:1,ShopName:PubName1,ShopAddress:1示例街道,exampleTown,邮政编码,ShopCategory: English},{ShopID:2,ShopName:PubName2,ShopAddress:2示例街道,exampleTown,邮政编码,ShopCategory:爱尔兰}]





如何获取数据并进行格式化,以便我可以单独输出第一家商店的ShopName等,以便显示。格式似乎在{}和FieldName中划分了一行:数据虽然我没有操纵这个的经验所以需要建议。



Text应转换为for循环内的变量并以可读格式显示。该地址是单独的字符串,所以我可以将它发送到谷歌地图API显示所以重要的是将它们分开



假设这是最好的方式我可以传递数据,如果有更好的方法请告诉我,虽然我想知道这个问题的解决方案,所以它不会再发生

解决方案

你可以使用 eval 方法以对象的形式获取数据。



do它是这样的:



  var  myObject =  eval ' (' +  document  .getElementById( <%= hfData.Value%>)。value + ' )'); 

alert(myObject.ShopID);
alert(myObject.PubName1);





同样,你可以使用你检索到的javascript对象的所有属性JSON。



PS 我建议您阅读javascript eval 文档以全面了解它。


 


document )。ready( function (){
var data = [{ ShopID 1 ShopName PubName1 ShopAddress 1示例street,exampleTown,邮政编码 ShopCategory English}, { ShopID 2 ShopName PubName2 ShopAddress 2示例街道,exampleTown,邮政编码,< span class =code-string> ShopCategory 爱尔兰}];

I am working on a project and am struggling after a lot of googling to find the best way to do this without using third party libraries. I would love some advise on this as its critical do getting my program correct.

My program is a C# webforms application with a Html/javascript front end. The C# side runs Sql queries on a Sql compact database and is meant send the data over to the javascript to be taken a part and loaded 1 row at a time.

I have a table of shops which has ID, Name,Address and Category fields.

        DataSet ds = new DataSet();
        adapter.Fill(ds);

        List<Shop> shops = new List<Shop>();

        foreach (DataRow row in ds.Tables[0].Rows)
        {

            string ID = row["ShopID"].ToString();
            string Name = row["ShopName"].ToString();
            string Address = row["ShopAddress"].ToString();
            string Category = row["Category"].ToString();

            shops.Add(new Shop(ID, Name, Address, Category));

        }

JavaScriptSerializer ser = new JavaScriptSerializer();
hfData.Value = ser.Serialize(shops);



This part seems to work perfectly, the data is loaded correctly and put into a hiddenfield called hfData.

While on the front end I have this line which is supposed to read the hiddenfield

  var HiddenValue = document.getElementById("<%=hfData.Value%>").value;
alert(HiddenValue);




The HiddenValue alert comes back in this format.

"[{"ShopID":"1","ShopName":"PubName1","ShopAddress":"1 Example street, exampleTown, postcode","ShopCategory":"English"},{"ShopID":"2","ShopName":"PubName2","ShopAddress":"2 Example street, exampleTown, postcode","ShopCategory":"Irish"}]"



How do I then take that data and formatted so I can output the ShopName etc for the first shop separately so it can be displayed. The format seems to group a row in {} and the "FieldName" : "Data" although I don''t have the experience to manipulate this so need advise.

The Text is meant to be converted to variables inside a for loop and displayed in a readable format. The address is meant to be separate string so I can send it to the google maps API to be displayed so its important to split them up

Assuming this is the best way I can pass data across, if there is a better way please tell me, although I would like to know the solution for this problem as well so It doesn''t occur again

解决方案

you can use the eval method to get the data in form of an object.

do it like this:

var myObject = eval('(' + document.getElementById("<%=hfData.Value%>").value + ')');

alert(myObject.ShopID);
alert(myObject.PubName1);



Similarly you can use all the properties of the javascript object which you have retrieved as JSON.

P.S. I suggest you read javascript eval documentation to get the complete understanding of it.



(document).ready(function () { var data = [{"ShopID":"1","ShopName":"PubName1","ShopAddress":"1 Example street, exampleTown, postcode","ShopCategory":"English"},{"ShopID":"2","ShopName":"PubName2","ShopAddress":"2 Example street, exampleTown, postcode","ShopCategory":"Irish"}];


这篇关于将c#数据传递给javascript并分解为可用格式的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆