C# HTML表单-如何处理动态数量的输入?

C# HTML表单-如何处理动态数量的输入?,c#,html,asp.net-mvc,C#,Html,Asp.net Mvc,我有一个网页要提交食谱。配料有5个字段。如果配方需要5种以上的配料,则会有一个按钮“添加另一种配料”,一些java脚本会加载另一种配料输入供他们使用。 我不知道如何将这些数据正确地发布到我的后端MVC4/C 我能给他们所有相同的id=Component并将它们收集为字符串ingrdient[]吗?或者,当输入的数量可能发生变化时,获取表单输入的最佳实践是什么 谢谢。如果您将使用方括号提供相同的名称,那么您应该能够在提交表单时以数组的形式访问它们 <input type="text" nam

我有一个网页要提交食谱。配料有5个字段。如果配方需要5种以上的配料,则会有一个按钮“添加另一种配料”,一些java脚本会加载另一种配料输入供他们使用。
我不知道如何将这些数据正确地发布到我的后端MVC4/C

我能给他们所有相同的id=Component并将它们收集为字符串ingrdient[]吗?或者,当输入的数量可能发生变化时,获取表单输入的最佳实践是什么


谢谢。

如果您将使用方括号提供相同的名称,那么您应该能够在提交表单时以数组的形式访问它们

<input type="text" name="data[]" value="">

如果您将使用方括号提供相同的名称,则在提交表单时应能够将其作为数组

<input type="text" name="data[]" value="">

就MVC4而言,我不知道确切的最佳方法是什么,但您完全可以在提交后给所有输入相同的名称,您应该能够通过查看
HttpContext.Request.Params
对象在服务器端检索它们。

就MVC4而言,我不知道确切的最佳方法是什么,但您完全可以在提交后以相同的名称提供所有输入,您只需查看
HttpContext.Request.Params
对象,就可以在服务器端检索它们。

确定以下是一个示例: 您可以使用以下方法:

 <div class="editor-field">
        <input type="text" name="MyModelArray[0].Sugar" value="" />
        <input type="text" name="MyModelArray[1].Tea" value="" />
        <input type="text" name="MyModelArray[0].Sugar" value="" />
        <input type="text" name="MyModelArray[1].Tea" value=""/>
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>


现在,您可以使用以下方法在控制器中获取此信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(List<Ingredient> MyModelArray) //Put a breakpoint here to see 
        {
            return View();
        }
        [HttpPost]
        public ActionResult About(List<string> MyStringArray) //Use this if you don't want a model
        {
            return View();
        }
    }
    public class Ingredient
    {
        public string Sugar { get; set; }
        public string Tea { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间MVCAPApplication1.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
[HttpPost]
public ActionResult Index(List MyModelArray)//在此处放置断点以查看
{
返回视图();
}
[HttpPost]
public ActionResult About(List MyStringArray)//如果不需要模型,请使用此选项
{
返回视图();
}
}
公共类成分
{
公共字符串Sugar{get;set;}
公共字符串Tea{get;set;}
}
}
好,这是一个示例: 您可以使用以下方法:

 <div class="editor-field">
        <input type="text" name="MyModelArray[0].Sugar" value="" />
        <input type="text" name="MyModelArray[1].Tea" value="" />
        <input type="text" name="MyModelArray[0].Sugar" value="" />
        <input type="text" name="MyModelArray[1].Tea" value=""/>
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>


现在,您可以使用以下方法在控制器中获取此信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(List<Ingredient> MyModelArray) //Put a breakpoint here to see 
        {
            return View();
        }
        [HttpPost]
        public ActionResult About(List<string> MyStringArray) //Use this if you don't want a model
        {
            return View();
        }
    }
    public class Ingredient
    {
        public string Sugar { get; set; }
        public string Tea { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间MVCAPApplication1.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
[HttpPost]
public ActionResult Index(List MyModelArray)//在此处放置断点以查看
{
返回视图();
}
[HttpPost]
public ActionResult About(List MyStringArray)//如果不需要模型,请使用此选项
{
返回视图();
}
}
公共类成分
{
公共字符串Sugar{get;set;}
公共字符串Tea{get;set;}
}
}

在数组中发布它将是最简单的解决方案。您可以使用控制器中的List或List一次获取所有数据,而不是通过FormController或HttpContext.Request.Params进行迭代。@Rajesh如何将它们全部作为数组发布?谢谢你的帮助。在数组中发布它将是最简单的解决方案。您可以使用控制器中的List或List一次获取所有数据,而不是通过FormController或HttpContext.Request.Params进行迭代。@Rajesh如何将它们全部作为数组发布?谢谢你的帮助。