我在asp.net中为我的数据库创建了四个模型类,其目的是创建一个调查问卷的模型,其中调查表有多个问题,每个问题可能有多个答案,答案有3种类型,多个答案用户只能选择一个答案,多个答案,用户可以选择多个答案,最后选择文本答案。一个问题只有一种答案类型。
我有一个ApiServices类,我在表中执行GET,然后调用类inqueritogViewModel上的gets,我想要对表进行内部连接,然后在设计器类上显示问题和答案。 / p>
我该怎么做?
感谢。
public class Inqueritog
{
[Key]
public int InqueritoId { get; set; }
[Required]
public string Name { get; set; }
public DateTime DateCreation { get; set; }
public DateTime ExpireDate { get; set; }
public int ExpireIn { get; set; }
}
public class Answerg
{
[Key]
public int AnswerId { get; set; }
[Required]
public string Answerv { get; set; }
public int QuestionId { get; set; }
}
public class TypeAnswerg
{
[Key]
public int TypeAnswerId { get; set; }
[Required]
public string TypesAnswer { get; set; }
}
public class Questiong
{
[Key]
public int QuestionId { get; set; }
public string Questionv { get; set; }
public int InqueritoId { get; set; }
public int TypeAnswerId { get; set; }
}
ApiServices类:
public class ApiServices
{
public async Task<bool> RegisterAsync(string email, string password, string confirmPassword)
{
var model = new RegisterBindingModel
{
Email = email,
Password = password,
ConfirmPassword = confirmPassword
};
using (var client = new HttpClient())
{
var json = JsonConvert.SerializeObject(model);
HttpContent content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync("http://192.168.58.30:17253/api/Account/Register", content);
return response.IsSuccessStatusCode;
}
}
public async Task<string> LoginAsync(string username, string password)
{
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("grant_type", "password")
};
var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.58.30:17253/Token");
request.Content=new FormUrlEncodedContent(keyValues);
var client = new HttpClient();
var response = await client.SendAsync(request);
var jwt = await response.Content.ReadAsStringAsync();
Settings.LoginSucess = response.IsSuccessStatusCode;
Settings.Username = username;
JObject jwtDynamic = JsonConvert.DeserializeObject<dynamic>(jwt);
var accessToken = jwtDynamic.Value<string>("access_token");
Settings.AccessToken = accessToken;
return accessToken;
}
public async Task<List<Inquerito>> GetinqueritosAsync(string acessToken1)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken1);
var json = await client.GetStringAsync("http://192.168.58.30:17253/api/Inqueritoes");
var inqueritos = JsonConvert.DeserializeObject<List<Inquerito>>(json);
return inqueritos;
}
public async Task<List<Inqueritog>> GetinqueritosgTaskAsync(string acessToken1)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken1);
var json = await client.GetStringAsync("http://192.168.58.30:17253/api/Inqueritogs/");
var inqueritos = JsonConvert.DeserializeObject<List<Inqueritog>>(json);
return inqueritos;
}
public async Task<List<Questiong>> GetQuestionsTaskAsync(string acessToken1)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken1);
var json = await client.GetStringAsync("http://192.168.58.30:17253/api/Questiongs");
var questions = JsonConvert.DeserializeObject<List<Questiong>>(json);
return questions;
}
public async Task<List<Answerg>> GetAnswerTaskAsync(string acessToken1)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken1);
var json = await client.GetStringAsync("http://192.168.58.30:17253/api/Answergs");
var Answers = JsonConvert.DeserializeObject<List<Answerg>>(json);
return Answers;
}
public async Task<List<TypeAnswerg>> GetTypeAnswerTaskAsync(string acessToken1)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken1);
var json = await client.GetStringAsync("http://192.168.58.30:17253/api/TypeAnswergs");
var TypeAnswers = JsonConvert.DeserializeObject<List<TypeAnswerg>>(json);
return TypeAnswers;
}
}
类inqueritogViewModel:
public class InqueritogViewModel : INotifyPropertyChanged
{
ApiServices _apiServices = new ApiServices();
public List<Inqueritog> _inqueritos;
private List<Questiong> _questions1;
private List<string> _inqueritosFinal;
private List<KeyValuePair<string, string>> kvpList = new List<KeyValuePair<string, string>>();
private List<Answerg> _answers;
private List<TypeAnswerg> _typeAnswers;
public string AccessToken { get; set; }
public List<Inqueritog> Inqueritos
{
get { return _inqueritos; }
set
{
_inqueritos = value;
OnPropertyChanged();
}
}
public List<string> inqueritos_Final
{
get { return _inqueritosFinal; }
set
{
_inqueritosFinal = value;
OnPropertyChanged();
}
}
public List<TypeAnswerg> TypeAnswers
{
get { return _typeAnswers; }
set
{
_typeAnswers = value;
OnPropertyChanged();
}
}
public List<Answerg> Answers
{
get { return _answers; }
set
{
_answers = value;
OnPropertyChanged();
}
}
public List<Questiong> Questions
{
get { return _questions1; }
set
{
_questions1 = value;
OnPropertyChanged();
}
}
public ICommand GetinqueritoCommand
{
get
{
return new Command(async () =>
{
AccessToken = Settings.AccessToken;
Questions = await _apiServices.GetQuestionsTaskAsync(AccessToken);
Inqueritos = await _apiServices.GetinqueritosgTaskAsync(AccessToken);
Answers = await _apiServices.GetAnswerTaskAsync(AccessToken);
TypeAnswers = await _apiServices.GetTypeAnswerTaskAsync(AccessToken);
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
设计问卷:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:App.ViewModels;assembly=App"
xmlns:controls="clr-namespace:Messier16.Forms.Controls;assembly=Messier16.Forms.Controls"
xmlns:xfControls="clr-namespace:AsNum.XFControls;assembly=AsNum.XFControls"
x:Class="App.ViewModels.InqueritoPage">
<ContentPage.BindingContext>
<viewModels:InqueritogViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Button Command="{Binding GetinqueritoCommand}" Text="Aceder a Questionarios"></Button>
<ListView x:Name="InqueritoView" ItemsSource="{Binding Inqueritos}" HasUnevenRows="True" ItemSelected="ListView_OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout >
<Label x:Name="Label1" Text="{Binding Pergunta}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<controls:Checkbox></controls:Checkbox>
<Label Text="{Binding Answer}" VerticalTextAlignment="Center"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<controls:Checkbox></controls:Checkbox>
<Label Text="{Binding Answer1}" VerticalTextAlignment="Center"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal" >
<controls:Checkbox></controls:Checkbox>
<Label Text="{Binding Answer2}" VerticalTextAlignment="Center"></Label>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>