如何重复表单控件在asp.net mvc的,并将它们传递作为一个列表到控制器 [英] How to repeat form controls in asp.net mvc and pass them as a list to controller

查看:194
本文介绍了如何重复表单控件在asp.net mvc的,并将它们传递作为一个列表到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个控件(一个TextBox一numericbox和日期)视图形式里面,这3种形式的模型对象。有向用户提供链接,在此链路的所有3控制重复的形式内的5倍的点击。

I have 3 controls(one textbox, one numericbox and a date) inside a view form, these 3 forms the Model object. There is link provided to the user, on click of this link all the 3 controls were repeated 5 times inside the form.

我在控制器的动作方法的参数类型的模型类的列表。如何克隆形式的内容的5倍,使其正确序列化到列表?

I have a list of Model class as an argument type in the controller action method. How to clone the form content 5 times such that it serializes properly to List?

推荐答案

一个简单的方法。

型号:

public class MyModel
{
    public IList<OneSetOfElements> FormSet { get; set; }
}

public class OneSetOfElements
{
    public string Name { get; set; }
    public int Age { get; set; }

    [DataType(DataType.Date)]
    public DateTime Dob { get; set; }
}

动作:

public ActionResult MyAction(MyModel model)
{
    foreach (var set in model.FormSet)
    {
        //read your data.
    }
}

查看:显示IDshowMeOnUrlClick上点击

View : Show the id "showMeOnUrlClick" on click

    @using (Html.BeginForm())
    {

//This would bind as first list element
        <div id="firstSet"> 
            @Html.EditorFor(m => m.FormSet[1].Name)
            @Html.EditorFor(m => m.FormSet[1].Age)
            @Html.EditorFor(m => m.FormSet[1].Dob, new { type = "date" })

        </div>

//Remaining list element
        <div id="showMeOnUrlClick" style="display: none"> 
            @for (int i = 1; i <= 6; i++)
            {
                @Html.EditorFor(m => m.FormSet[i].Name)
                @Html.EditorFor(m => m.FormSet[i].Age)
                @Html.EditorFor(m => m.FormSet[i].Dob, new {type = "date"})
            }
        </div>

        <button onclick="showTheRepeatedFields() ">Show</button>
    }

这篇关于如何重复表单控件在asp.net mvc的,并将它们传递作为一个列表到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆