如何从模型中获取绑定数据到后面的代码

时间:2017-11-05 11:48:07

标签: php json visual-studio xamarin xamarin.forms

我有一个单独的模型来从SQLserver获取票证ID,如下所示(我从PHP获取数据作为json对象)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject.Data
{
    public class TiketViewModel
    {
        public int TiketNumb { get; set; }
        public int FlagNum { get; set; }
    }

    public class JsonTicketNewNum
    {
        public List<TiketViewModel>TicketInfo { get; set; }
    }
}

我通过使用post async获取数据通过模型(我发送位置ID和表id作为输入以从sql服务器检索票证ID)

public async void GetTicketinfo()
{
    string LocationId = "1";
    var client = new HttpClient();
    var postData = new List<KeyValuePair<string, string>>();
    postData.Add(new KeyValuePair<string, string>("LocationId", LocationId));
    postData.Add(new KeyValuePair<string, string>("TableID", count));////////
    var content = new FormUrlEncodedContent(postData);
    // var response = await client.PostAsync("http://192.168.43.226/GetTicket.php", content);
    var response = await client.PostAsync(Constants.BaseUrlpos + "GetTicket.php", content);
    string JsonTiket = response.Content.ReadAsStringAsync().Result;
    // string JsonTikettwo = response.Content.ReadAsStringAsync().Result;
    // TicketModel ObjContactList = new TicketModel();
    TiketViewModel ObjContactList = new TiketViewModel();
    if (response.IsSuccessStatusCode)
    {
        ObjContactList = JsonConvert.DeserializeObject(JsonTiket);
        var items = ObjContactList.TiketNumb;
        var itemstwo = ObjContactList.FlagNum;
    }

我的问题是如何通过模型数据集访问TiketNumbFlagNum后面的代码来检索O的数据?因为Issucessstatuscode中存在例外情况。在此先感谢您的支持。 泛

1 个答案:

答案 0 :(得分:0)

我会稍微更改一下代码。我在检查内容之前检查是否是响应IsSuccessStatusCode

var response = await client.PostAsync(Constants.BaseUrlpos + "GetTicket.php", content);

// string JsonTikettwo = response.Content.ReadAsStringAsync().Result;
// TicketModel ObjContactList = new TicketModel();
TiketViewModel ObjContactList = new TiketViewModel();
if (response.IsSuccessStatusCode)
{
   string JsonTiket = await response.Content.ReadAsStringAsync();
   ObjContactList = JsonConvert.DeserializeObject<TiketViewModel>(JsonTiket);
   var items = ObjContactList.TiketNumb;
   var itemstwo = ObjContactList.FlagNum;
}