【发布时间】:2023-04-18 07:38:01
【问题描述】:
我正在尝试将某些对象添加到另一个对象。但我在“选项”部分收到错误消息。我只是想将某些东西从一个对象中添加到另一个对象中。
这是我的代码的样子..
var responses = new Responses();
form.Questions.ForEach(
q => responses.Questions.Add(new Models.Question()
{
QuestionId = Convert.ToInt32(q.Id),
Value = q.SingleAnswer,
Options = q.Options.ForEach( o => q.Options.Add(
new Option // <----FAILING HERE!!!!!!!!!!!!
{
OptionId = 1,
Value = "test"
}
))
})
);
错误是
Argument type 'Web.Models.Option' is not assignable to parameter type QuestionOptionViewModel
型号:
public class Responses
{
public List<Question> Questions { get; set; }
}
public class Question
{
public int QuestionId { get; set; }
public string Value { get; set; }
public List<Option> Options { get; set; }
}
public class Option
{
public int OptionId { get; set; }
public string Value { get; set; }
}
public class QuestionOptionViewModel
{
public int? Id { get; set; }
public string Text { get; set; }
public string QuestionType { get; set; }
[RequiredIf("QuestionType", "text", ErrorMessage = "Required Field")]
public string Value { get; set; }
[RequiredIf("QuestionType", "checkbox", ErrorMessage = "Required Field")]
public bool IsChecked { get; set; }
}
public class QuestionViewModel
{
public int? Id { get; set; }
public string QuestionType { get; set; }
public string SubType { get; set; }
public string Text { get; set; }
public int SortOrder { get; set; }
public bool IsHidden { get; set; }
[RequiredIf("QuestionType", "singleAnswer", ErrorMessage = "Reqired Field")]
public string SingleAnswer { get; set; }
[RequiredIf("QuestionType", "radio", ErrorMessage = "Radio Reqired")]
public int? SelectedRadio { get; set; }
[RequiredIf("QuestionType", "select", ErrorMessage = "Selection Reqired")]
public int? SelectedSelect { get; set; }
public bool CheckboxError { get; set; }
public List<QuestionOptionViewModel> Options { get; set; }
}
【问题讨论】:
标签: c# asp.net-mvc linq list lambda