简体   繁体   English

ASP.NET MVC4-在多个文本框中添加多个

[英]ASP.NET MVC4 - Multiple add in multiple textboxes

I'm working with ASP.NET MVC4. 我正在使用ASP.NET MVC4。 I want to offer the possibility to add multiples records (from 1 to 3 maximum) by displaying a view with multiple textboxes which finally represents the same things. 我想通过显示带有多个文本框的视图来添加多个记录(从1到3个最大值),最终代表相同的内容。

For instance, in my View, I would like to have something like this : 例如,在我的视图中,我想要这样的东西:

Start hour (1) : |TextBoxForHours| 开始时间(1):|| TextBoxForHours | h |TextBoxForMinutes| h | TextBoxForMinutes |

Start hour (2) : |TextBoxForHours| 开始时间(2):|| TextBoxForHours | h |TextBoxForMinutes| h | TextBoxForMinutes |

Start hour (3) : |TextBoxForHours| 开始时间(3):| TextBoxForHours | h |TextBoxForMinutes| h | TextBoxForMinutes |

In my controller, I'll check how many textboxes have been filled and save it in my db. 在控制器中,我将检查已填充了多少个文本框并将其保存在数据库中。

Here's the ViewModel I've created to do that : 这是我为此创建的ViewModel:

public class DeniedIntervalsViewModel
{
    public int StartHour { get; set; }

    public int StartMinute { get; set; }


}

Any idea to do that? 有什么想法吗?

you can use input naming convention for Array, List, Collection or dictionary like this 您可以像这样对数组,列表,集合或字典使用输入命名约定

[Updates] [更新]

Oneway inputs: 单向输入:

@for (int i = 0; i < Model.Count; i++)
{
    <div>
        <label>Start hour :</label>
        <input type="text" name="intervals[@i].StartHour " value="" />
        <input type="text" name="intervals[@i].StartMinute " value="" />
    </div>
}

To bind ViewModel back to View: 要将ViewModel绑定回View:

@for (int i = 0; i < Model.Count; i++)
{
    <div>
        <label>Start hour :</label>
        @Html.TextBoxFor(model => model.Intervals[i].FirstName)
        @Html.TextBoxFor(model => model.Intervals[i].LastName)
    </div>
}

and your parent ViewModel contains array 并且您的父ViewModel包含数组

public class TheViewModel{

    public DeniedIntervalsViewModel[] Intervals{ get; set; }
}

public class TheController{

    public ActionResult Blah(DeniedIntervalsViewModel[] intervals) {
      // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM