Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# 在发布表单之前,将我的所有文本框值聚合到一个列表中_C#_Asp.net_Html_Asp.net Mvc 3_Razor - Fatal编程技术网

C# 在发布表单之前,将我的所有文本框值聚合到一个列表中

C# 在发布表单之前,将我的所有文本框值聚合到一个列表中,c#,asp.net,html,asp.net-mvc-3,razor,C#,Asp.net,Html,Asp.net Mvc 3,Razor,我正在用一堆文本框构建一个MVC表单。但是,我的模型需要一个字符串列表。我试图做到的是,当用户点击submit时,在表单发布之前,它会获取文本框中的所有值,将其添加到列表中,并将其附加到模型中进行提交。我一直在想我怎么才能做到这一点 我之所以需要这样做,是因为我需要表单是动态的,因为字段的数量可以增加,页面可以适应这种情况。我无法将各个字段硬编码到模型中,因为模型需要适应表单的长度 以下是我目前的代码: HTML @using (Html.BeginForm(new { id = "feedba

我正在用一堆文本框构建一个MVC表单。但是,我的模型需要一个字符串列表。我试图做到的是,当用户点击submit时,在表单发布之前,它会获取文本框中的所有值,将其添加到列表中,并将其附加到模型中进行提交。我一直在想我怎么才能做到这一点

我之所以需要这样做,是因为我需要表单是动态的,因为字段的数量可以增加,页面可以适应这种情况。我无法将各个字段硬编码到模型中,因为模型需要适应表单的长度

以下是我目前的代码:

HTML

@using (Html.BeginForm(new { id = "feedbackForm" }))
{
<fieldset>

    @foreach (Feedback_Num_Questions questionForm in FeedbackSurveyQuestions)
    {
        string textBoxID = "feedbackq";

        questionNumberTextBoxes++;

        textBoxID = textBoxID + questionNumberTextBoxes.ToString();

        <input type="text" id="@textBoxID" />
    }

    <input type="text" id="txtAdditionalComments" />

    <input type="submit" value="Create" name="btnFeedbackSubmit" id="btnSubmitFeedbackForm" />

    </fieldset>
}

我相信,如果您修改文本框的id和name属性,并且您的post操作期望您的模型类作为其参数,那么您应该能够在创建文本框时执行以下操作:

<input type="text" id="id="responseList_@textBoxID__" name="responseList[@textBoxID]" />

我会让模型绑定器为您完成这项艰巨的工作

@using (Html.BeginForm(new { id = "feedbackForm" }))
{
    <fieldset>
    @{
        int i = 0;
        foreach (Feedback_Num_Questions questionForm in FeedbackSurveyQuestions)
        {
            @Html.EditorFor(m => m.responseList[i++])
        }
    }

        <input type="text" id="txtAdditionalComments" />

        <input type="submit" value="Create" name="btnFeedbackSubmit" id="btnSubmitFeedbackForm" />

    </fieldset>
}
@使用(Html.BeginForm(新的{id=“feedbackForm”}))
{
@{
int i=0;
foreach(反馈调查问题中的反馈数量问题调查表)
{
@EditorFor(m=>m.responseList[i++]
}
}
}
以下是一个工作示例:

型号和控制器:

using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class MyController : Controller
    {
        public ActionResult MyAction(MyModel Model)
        {
            return View(Model);
        }

    }
    public class MyModel
    {
        public List<string> TextFields { get; set; }
    }
}
使用System.Collections.Generic;
使用System.Web.Mvc;
命名空间MVCAPApplication1.Controllers
{
公共类MyController:Controller
{
公共行动结果MyAction(MyModel模型)
{
返回视图(模型);
}
}
公共类MyModel
{
公共列表文本字段{get;set;}
}
}
视图:

@model mvcapapplication1.Controllers.MyModel
@使用(Html.BeginForm())
{
对于(int i=0;i<3;i++)
{
string id=“txt”+i.ToString();
@Html.TextBoxFor(m=>m.TextFields[i],新的{id=id})
}
如果(Model.TextFields!=null)
{
foreach(Model.TextFields中的字符串textField)
{ 
已发布字段:@textField
}
}
}

嘿,这看起来很有希望,但是我需要能够自定义文本框的ID。编辑不允许我。我用TextBoxFor做了同样的事情,它给了我正确的ID名称,但是在提交表单时,列表中没有数据……您可以使用TextBoxFor:@Html.TextBoxFor(m=>m.responseList[I++],new{ID=textBoxID})出于某种原因,我仍然无法使用它:(太接近了。我明天会让办公室里更资深的人看一下,看看他是否能找出问题所在。不过非常感谢,你肯定给了我更好的视角来解决这个问题。如果你发现了问题的根源,请发布它!:够狡猾的,我今天刷新了代码,它成功了。Y我们的解决方案一直都是正确的。您的操作是什么样子的?我将扩展原始帖子以包含操作代码…将I++放在原始示例的方括号中返回了一个错误,因此我必须在外部执行I++操作,并且我正在使用该值作为命名约定。做同样的事情。。。。
<input type="text" id="id="responseList_@textBoxID__" name="responseList[@textBoxID]" />
@using (Html.BeginForm(new { id = "feedbackForm" }))
{
    <fieldset>
    @{
        int i = 0;
        foreach (Feedback_Num_Questions questionForm in FeedbackSurveyQuestions)
        {
            @Html.EditorFor(m => m.responseList[i++])
        }
    }

        <input type="text" id="txtAdditionalComments" />

        <input type="submit" value="Create" name="btnFeedbackSubmit" id="btnSubmitFeedbackForm" />

    </fieldset>
}
using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class MyController : Controller
    {
        public ActionResult MyAction(MyModel Model)
        {
            return View(Model);
        }

    }
    public class MyModel
    {
        public List<string> TextFields { get; set; }
    }
}
@model MvcApplication1.Controllers.MyModel

@using (Html.BeginForm())
{
    for (int i = 0; i < 3; i++)
    {
        string id = "txt" + i.ToString();

        @Html.TextBoxFor(m => m.TextFields[i], new { id = id })
    }

    <input type="submit" value="Post" />

    if (Model.TextFields != null)
    {
        foreach (string textField in Model.TextFields)
        { 
            <div>Posted Field: @textField</div>
        }
    }
}