如何根据mvc4中的dropdownlist选择值检查“for”循环中的条件

时间:2014-12-26 11:14:49

标签: asp.net-mvc asp.net-mvc-4

根据从下拉列表中选择的计数,我必须动态生成表行。例如,如果count为3,则必须生成3个表行(每个表行有4个表数据)。请任何人帮我实现这种情况吗?

以下是查看代码:

<table id="dynamiccount">
    <tr> 
        <td>
            <div class="editor-label">
                        @Html.Label("please select count")
                </div>
            </td> 
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.count,@ViewBag.dynamic as SelectList,"select")
                        @Html.ValidationMessageFor(model => model.count)
                </div>

            </td>
    </tr>
</table>

<table id="dynamic">

    <tr> 
        <td> 
                <div class="editor-label">
                        @Html.LabelFor(model => model.value1)
                </div>
        </td>
        <td>
                <div class="editor-field">
                     @Html.DropDownListFor(model => model.value1, @ViewBag.Value1 as SelectList, "select")
                        @Html.ValidationMessageFor(model => model.value1)
                </div>
            </td>
            <td> 
                    <div class="editor-label">
                            @Html.LabelFor(model => model.value2)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.value2, @ViewBag.Value2 as SelectList, "select")
                            @Html.ValidationMessageFor(model => model.value2)
                    </div>  
        </td>
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.value3, @ViewBag.Value3 as SelectList, "select")
                        @Html.ValidationMessageFor(model => model.value3)
                </div>
            </td>
    </tr>


     </table>

现在,基于从dropdownlist中选择的model.count值,我需要相应地生成表行。

1 个答案:

答案 0 :(得分:0)

您需要的是一个简单的FOR:

for (int i = 1; i <= countDropDown; i++)
{
    //Add one Row with its data
}

由于你没有提供你的代码......答案是超级通用的。

<强>更新

选择DropDownList是客户端事件。因此,您需要将所选值传递给服务器,并返回“行”部分的HTML。为此,在Javascript中,您需要绑定DropDownList的onChange事件以获取所选值。然后......你向服务器“请求行”并传递行号。

另外,你可以在客户端做到这一切...在客户端生成行...但在这种情况下我怀疑你在发布用户时会出现问题(并非不可能,但很棘手)将这些行输入到服务器。

您的HTML:

    <table id="dynamiccount">
        <tr> 
            <td>
                <div class="editor-label">
                            @Html.Label("please select count")
                    </div>
                </td> 
                <td>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.count,(SelectList)ViewBag.dynamic,"select")
                            @Html.ValidationMessageFor(model => model.count)
                    </div>

                </td>
        </tr>
    </table>

<div id="placeholderForTable">

<div>

使用jQuery:

$( "#select" ).change(function() {
   var countSelected = $(this).val();

   $.ajax({
    type: 'GET',
    url: 'UrlToAction', //this Action need to return a PartialView
    data: "count=" + countSelected ,
    success: function(data){
        alert('successful');
        $('#placeholderForTable').html(data); //the HTML of the view is placed in the Div whose id is "placeHolderForTable"
    }
});
});

控制器操作:

这由你来代码:)

免责声明:所有代码都写在这里,没有经过测试。