1

它是正确的模型吗?

public class NewForm
{
    public string[] Field { get; set; }
    public bool[] Check { get; set; }
}

对于这样的视图:

@Html.TextAreaFor(model => model.Field)
@Html.TextAreaFor(model => model.Field)
@Html.TextAreaFor(model => model.Field)

@Html.CheckBoxFor(model => model.Check)
@Html.CheckBoxFor(model => model.Check)

还是有更好的方法来创建同名字段?在 Controller 中仅显示第一个值。但我需要所有

        [HttpPost]

    public ActionResult Edit(NewForm model)
    {
        Response.Write(model.Field);
        Response.Write(model.Check);
    }

字段可能是不确定的数字,因为通过单击按钮 JavaScript 会添加一个具有相同名称的同名新字段

4

3 回答 3

1

听起来您想将model背部的多个实例提交给控制器。

你可以做这样的事情。我的示例将提交 10 个实例Field返回给您的控制器。

看法:

@using (Html.BeginForm())
{
    <div>
    @for(int i = 0; i<10; i++) 
    {
        <div>@Html.TextBox("items[" + i + "].Field", "", new { id = "items[" + i + "].Field", placeholder = "Enter Text..." })</div>
        @Html.Hidden("items.Index", i)
    }
    </div>
    <input type="submit" value="Submit" />
}

班级:

public class MyClass 
{
    public string Field {get;set;}
}

控制器方法:

[HttpPost]
public ActionResult ActionName(List<MyClass> items)
{
   //...do stuff
}

显然,您也可以将复选框添加到模型和表单中,以便提交其中的许多。

于 2019-02-08T15:26:32.803 回答
0

为什么要为字段使用相同的名称,每个字段都有正确的名称

public class NewForm
{
    public string FirstField { get; set; }
    public string Field { get; set; }
    public bool Check { get; set; }
}

看法

@Html.TextAreaFor(model => model.FirstField)
@Html.TextAreaFor(model => model.Field)

@Html.CheckBoxFor(model => model.Check)

ee

[HttpPost]
public ActionResult Edit(NewForm model)
{
    Response.Write(model.FirstField);
    Response.Write(model.Field);
    Response.Write(model.Check);
}
于 2019-02-08T14:57:52.393 回答
0

不,不是。在您定义的模型中,您创建了两个不同的数组:第一个属性Field是字符串数组,第二个属性Check是布尔数组。把[]类型放在后面表示一个数组。

如果你有未知数量的我称之为“迷你表单”,并且这些数量是由用户通过 UI 决定的,那么你应该创建一个视图模型来表示这个迷你表单,并创建一个容器视图模型来容纳它和您的视图需要的任何其他属性。

例如:

public class MiniFormViewModel
{
    public string MyInput { get; set; }
    public bool MyCheck { get; set; }
}

然后在您的容器视图模型中:

public class ContainerViewModel
{ 
    public IEnumerable<MiniFormViewModel> MiniForms { get; set; } 
    //Any other properties you need on the view that will occur a single time 
}

现在,在 JS 中,您需要添加一些操作才能做到这一点:

function getViewModel() {
    //You'll have to decide how you want to get the values of the mini form's fields. Perhaps you might even have a function to supply these values. Up to you.
    return {
        MiniForms: [{
                MyInput: '', //value from the first "mini form' string field
                Mycheck: false //value from the first "mini-form" bool field
            },
            {
                MyInput: '', //value from the second"mini form' string field
                Mycheck: false //value from the second"mini-form" bool field
            }      
        ]
    }
}

然后,您需要将其发布回服务器。我将通过内置的 JS Fetch 函数演示如何做到这一点:

        fetch(yourUrlForTheControllerAction,
            {
                method: 'post',
                body: JSON.stringify(getViewModel()),
                headers: {
                    'content-type': 'application/json; charset=UTF-8'
                }
            })

然后 blammo,你应该很高兴。我排除了动态添加迷你表单字段的部分,因为听起来您已经有了解决方案。

于 2019-02-08T14:58:57.617 回答