Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web API参数是否将动态生成的表值绑定到模型?_C#_Asp.net Web Api_Model_Parameterbinding - Fatal编程技术网

C# Web API参数是否将动态生成的表值绑定到模型?

C# Web API参数是否将动态生成的表值绑定到模型?,c#,asp.net-web-api,model,parameterbinding,C#,Asp.net Web Api,Model,Parameterbinding,假设您正在创建一个在线测试应用程序。它允许您添加多项选择题。因此,用户单击“添加新的测试问题”,就会出现一个对话框。对话框要求输入问题文本、可能答案列表和正确答案。这样的结果就是: Which color has the letter "G" in it? A. Blue B. Red ----> C. Green D. Yellow E. Purple 每个新问

假设您正在创建一个在线测试应用程序。它允许您添加多项选择题。因此,用户单击“添加新的测试问题”,就会出现一个对话框。对话框要求输入问题文本、可能答案列表和正确答案。这样的结果就是:

    Which color has the letter "G" in it?
            A.  Blue
            B.  Red
    ---->   C.  Green
            D.  Yellow
            E.  Purple
每个新问题都可能有不同数量的选项。因此,下一个问题可能是:

    Does NYC have 5 boroughs?
    --->    A.  Yes
            B.  No

我创建了一个对话框,允许用户在表单中动态生成这些问题(添加答案、指定正确答案等)。是否需要创建一个模型和一个Web API来无缝地在表单提交时绑定该结构?我在想一些疯狂的事情,比如,如果我的表单中有一个表,我可以以某种方式将它绑定到模型中的数组?可能不是那样的,但是在寻找一个创造性的想法。

一个模型可能看起来像这样

public class Question {
    public string Text { get; set;}
    public IList<Answer> Answers { get; set;}
}

public class Answer {
    public string Label { get; set;}
    public string Text { get; set;}
    public bool IsCorrect { get; set;}
}
公开课问题{
公共字符串文本{get;set;}
公共IList答案{get;set;}
}
公开课答案{
公共字符串标签{get;set;}
公共字符串文本{get;set;}
公共bool IsCorrect{get;set;}
}
api端点将使用这些元素的集合来形成测试

public class Test {
    public IList<Question> Questions { get; set; }
}

public class TestController : ApiController {
    [HttpPost]
    public IHttpActionResult Create(Test test) { ... }
}
公共类测试{
公共IList问题{get;set;}
}
公共类TestController:ApicController{
[HttpPost]
公共IHttpActionResult创建(测试){…}
}

模型可能看起来像这样

public class Question {
    public string Text { get; set;}
    public IList<Answer> Answers { get; set;}
}

public class Answer {
    public string Label { get; set;}
    public string Text { get; set;}
    public bool IsCorrect { get; set;}
}
公开课问题{
公共字符串文本{get;set;}
公共IList答案{get;set;}
}
公开课答案{
公共字符串标签{get;set;}
公共字符串文本{get;set;}
公共bool IsCorrect{get;set;}
}
api端点将使用这些元素的集合来形成测试

public class Test {
    public IList<Question> Questions { get; set; }
}

public class TestController : ApiController {
    [HttpPost]
    public IHttpActionResult Create(Test test) { ... }
}
公共类测试{
公共IList问题{get;set;}
}
公共类TestController:ApicController{
[HttpPost]
公共IHttpActionResult创建(测试){…}
}

感谢您的回复。我认为我的问题更多的是表单提交,而不是模型结构本身。我知道WebAPI有参数绑定。基本上,如果web表单中的字段包含名称属性,并且名称属性值与服务器端模型字段名称匹配,web API将自动将表单元素值映射到服务器端模型值。这对于静态表单字段非常有效。但在本例中,答案元素是动态生成的,并且数量不同。如何在提交时将动态创建的表单字段映射到模型?将集合作为数组或集合提交,ModelBinder将转换它们。感谢您的回复。我认为我的问题更多的是表单提交,而不是模型结构本身。我知道WebAPI有参数绑定。基本上,如果web表单中的字段包含名称属性,并且名称属性值与服务器端模型字段名称匹配,web API将自动将表单元素值映射到服务器端模型值。这对于静态表单字段非常有效。但在本例中,答案元素是动态生成的,并且数量不同。如何在提交时将动态创建的表单字段映射到模型?将集合作为数组或集合提交,ModelBinder将转换它们。