当我尝试将数据从视图传递到控制器时,调试模式下的参数在定义为List时显示为null(在save方法中)。
以下是控制器中尝试保存数据时获取异常的方法。当它们只是对象(类别类别而不是List< Category>类别)时,它们返回第一个数据实例,但不是null。
public class NewSurveyViewModel
{
public List<Category> Categories { get; set; }
public List<Question> Questions { get; set; }
public List<Answer> Answers { get; set; }
[Display(Name = "Survey Name")]
public int? SurveyId { get; set; }
[Required(ErrorMessage = "The survey name cannot be blank")]
public string Description { get; set; }
public NewSurveyViewModel()
{
SurveyId = 0;
}
}
以及从中发送数据的观点。我循环遍历所有类别,类别中的问题以及问题的答案。我甚至不确定我是否真的需要HiddenFields。
<ContentPage.Content>
<ListView x:Name="___list" Margin="50" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding CellTap}" />
</StackLayout.GestureRecognizers>
<Label Text="{Binding Title}" />
<Label Text="{Binding Detail}" FontSize="30" IsVisible="{Binding ShowDetails}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
我的猜测是,视图中的某个位置数据未正确发送到控制器,否则它不会为空。此问题是否源于从视图到控制器的错误发送?
这里使用的Class(视图模型)是
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
___list.ItemsSource = new List<Element>() {
new Element() {
Title="First Element",
Detail = "First Element Details"
},
new Element() {
Title="Second Element",
Detail = "Second Element Details"
}
};
}
}
public class Element : INotifyPropertyChanged
{
public Element()
{
CellTap = new Command(() =>
{
ShowDetails = !ShowDetails;
});
}
public ICommand CellTap { get; private set; }
private string _title;
public string Title
{
get { return _title; }
set { if (_title != value) { _title = value; OnPropertyChanged("Title"); } }
}
private string _detail;
public string Detail
{
get { return _detail; }
set { if (_detail != value) { _detail = value; OnPropertyChanged("Detail"); } }
}
private bool _showDetails;
public bool ShowDetails
{
get { return _showDetails; }
set { if (_showDetails != value) { _showDetails = value; OnPropertyChanged("ShowDetails"); } }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我为巨大的代码块道歉,但我认为这一切都是相关的,并且不想留下任何东西以获得更好的图片。