我正在开发一个服务基础设施和管理控制面板,两者都基于 ServiceStack(管理面板是 ServiceStack + Razor)。在管理 View 中,我创建了一个具有两个不同索引字段的表单:
<input name="StepName[]" />
<input name="StepDescription[]" />
<input name="StepDuration[]" />
<input name="IngredientName[]" />
<input name="IngredientQuantityName[]" />
<input name="IngredientQuantityValue[]" />
然后我写了管理员请求DTO:
public class StuffCreateRequest
{
public string Name { get; set; }
public string[] StepName { get; set; }
public string[] StepDescription { get; set; }
public int[] StepDuration { get; set; }
public string[] IngredientName { get; set; }
public string[] IngredientQuantityName { get; set; }
public double[] IngredientQuantityValue { get; set; }
}
但是所有的数组属性都是空的。
问题是如何从这些字段中获取值?
附言实际上,知道如何从表单接收复合 DTO(带有列表、字典等)会更好吗?
最佳答案
我已经创建了一个完整的演示,演示如何处理从表单发布的集合数据。 ServiceStack 能够将表单数据转换为简单的集合,但无法处理复杂的结构,除非您使用 JSON 等数据类型。
为了解决这个问题,我会以更扁平的格式发布数据,然后让服务器将数据扩展为更结构化的格式,以便在您的代码中使用。
Full Source Code Here.
HTML 表单:
示例编辑器,显示多种成分和步骤。
结果:
显示从 HTML 表单发送的扩展的结构化数据。
方法:
在 HTML 中使用平面变量名:
即命名变量 StepName
而不是 StepName[]
<input type="text" name="StepName" value="1 - Prepare" />
...
<input type="text" name="StepName" value="2 - Mix" />
设置表单类型:
如果您使用的是自托管应用,我注意到如果没有明确的类型集,表单数据不会被填充。
<form action="/Recipes" method="POST" enctype="multipart/form-data">
设置您的 Request 对象以期待收集
[Route("/Recipes", "POST")]
public class CreateRecipeRequest : IReturn<Recipe>
{
public string Name { get; set; }
public string[] StepName { get; set; }
public string[] StepDescription { get; set; }
public int[] StepDuration { get; set; }
public string[] IngredientName { get; set; }
public string[] IngredientQuantityName { get; set; }
public decimal[] IngredientQuantityValue { get; set; }
}
将扁平化请求转换为结构化配方:
public class Recipe
{
public string Name { get; set; }
public Ingredient[] Ingredients { get; set; }
public Step[] Steps { get; set; }
}
public class Ingredient
{
public string Name { get; set; }
public string QuantityName { get; set; }
public decimal Quantity { get; set; }
}
public class Step
{
public string Name { get; set; }
public string Description { get; set; }
public int Duration { get; set; }
}
public class RecipeService : Service
{
public Recipe Post(CreateRecipeRequest request)
{
// Convert the flat CreateRecipeRequest object to a structured Recipe object
// Steps
int numberOfSteps = request.StepName.Length;
// Ingredients
int numberOfIngredients = request.IngredientName.Length;
// Check there is a description and duration for all steps
if(request.StepDescription == null || request.StepDescription.Length != numberOfSteps || request.StepDuration == null || request.StepDuration.Length != numberOfSteps)
throw new Exception("There must be a duration and description for all steps");
// Create the Recipe object
var recipe = new Recipe { Name = request.Name, Steps = new Step[numberOfSteps], Ingredients = new Ingredient[numberOfIngredients] };
for(int s = 0; s < numberOfSteps; s++)
recipe.Steps[s] = new Step { Name = request.StepName[s], Description = request.StepDescription[s], Duration = request.StepDuration[s] };
// Check there is a quantity type and quantity value for all ingredients
if(request.IngredientQuantityName == null || request.IngredientQuantityName.Length != numberOfIngredients || request.IngredientQuantityValue == null || request.IngredientQuantityValue.Length != numberOfIngredients)
throw new Exception("The quantity must be provided for each ingredient");
for(int i = 0; i < numberOfIngredients; i++)
recipe.Ingredients[i] = new Ingredient { Name = request.IngredientName[i], QuantityName = request.IngredientQuantityName[i], Quantity = request.IngredientQuantityValue[i] };
/*
* Recipe can now be accessed through a logical collection:
*
* recipe.Name
* recipe.Steps[0].Name
* recipe.Ingredients[1].Quantity
*
*/
return recipe;
}
}
关于c# - ServiceStack 中的索引输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21437792/