如何使用HiddenField从MVC中的下拉列表Telerik获取文本值

时间:2013-03-27 07:57:43

标签: asp.net-mvc entity-framework

在我的MVC(Razor)项目中,我有一个疑问。 我有一个名为Hierarchy.cshtml的Razor视图

查看:

@model List<DatabaseModel.SampleWizardModel>
<script>
    $(document).ready(function () {
        $('.chk-act').click(function () {
            if ($(this).is(':checked')) {
                $('#AttributeList' + this.value).data("tDropDownList").enable();
            }
            else {
                $('#AttributeList' + this.value).data("tDropDownList").disable();
            }
        });
    });

    function SetAllCheckBoxes(obj) {
        var c = new Array();
        c = document.getElementsByTagName('input');
        for (var i = 0; i < c.length; i++) {
            if (c[i].type == 'checkbox') {
                c[i].checked = obj.checked;
            }
        }
        $('.chk-act').each(function () {
            if ($(this).is(':checked')) {
                $('#AttributeList' + this.value).data("tDropDownList").enable();
            }
            else {
                $('#AttributeList' + this.value).data("tDropDownList").disable();
            }
        });
    }

    function onAttListChange(e) {
        $('#hdattid').val($("#" + this.id).data("tDropDownList").text());
       // $get("tDropDownList").value = e.get_item().get_value();
    }

</script>

<div>
    <table border="0">
        <tr>
            <th align="center">Colum Name</th>
            <th align="center" width="150px">Import</br>(<input type="checkbox" name="chkselectall" onclick="SetAllCheckBoxes(this)" /><i>Select All </i>)</th>
            <th align="center">Field Mapping</th>
        </tr>
        @using (Html.BeginForm("SampleImport", "Sample", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            foreach (var item in Model)
            {
                string checkbox = "actionChk" + item.Index;
            <tr>
                <td>@item.ColumName</td>
                <td align="center">
                    <input type="checkbox" class="chk-act" id="@checkbox" value="@item.Index" name="@checkbox"></td>
                <td>
                    <div id="dd">@(Html.Telerik().DropDownListFor(m => m.FirstOrDefault().DropDownAttributes).Name("AttributeList" + item.Index).BindTo(item.DropDownAttributes).ClientEvents(e =>e.OnChange("onAttListChange")).HtmlAttributes(new { style = "width:235px" }).Enable(false))</div>
                    @Html.Hidden("hdattid" + item.Index, item.DropDownAttributes.FirstOrDefault().Text)
                </td>
            </tr>
            }
            <tr>
                <td></td>
                <td></td>
                <td><b><i>@Html.CheckBox("Allow Duplicate", false) Allow Duplicate</i></b>
                    <input type="submit" value="Start Import" /></td>
            </tr>
        }

    </table>

</div>

我的模特是:

#region SampleWizard Model Class
    public class SampleWizardModel
    {
        public int Index { get; set; }

        public string ColumName { get; set; }

        public bool Selected { get; set; }

        public int AttributrId { get; set; }

        public List<SelectListItem> DropDownAttributes { get; set; }

        public int AttributeTypeId { get; set; }

        public string AttributeName { get; set; }

    }
    #endregion

控制器:

[HttpPost]
    public ActionResult FinalImport(FormCollection collection)
    {
    List<DatabaseModel.HierarchyWizardModel> model = (List<DatabaseModel.HierarchyWizardModel>)Session["HierarchyWizardModel"];

    foreach (var key in collection.AllKeys)
    {
        if (key.Contains("actionChk"))
        {
            int Index = int.Parse(Request[key].ToString());
            var Item = model.Where(h => h.Index == Index).FirstOrDefault();
            Item.Selected = true;
            string Value = Request["AttributeList" + Index].ToString();
            if (Value == "Category" || Value == "SubCategory" || Value == "CatalogItemNo")
            {
                Item.AttributeName = Request["hdattid" + Index].ToString();
                Item.AttributrId = 0;
                Item.AttributeTypeId = 0;
            }
            else
            {
                Item.AttributeName = Request["hdattid" + Index].ToString();
                if (Request["hdattid" + Index].ToString().Contains("New Attribute"))
                {
                    Item.AttributeTypeId = int.Parse(Value);
                    Item.AttributrId = 0;
                }
                else
                {
                    Item.AttributrId = int.Parse(Value);
                    Item.AttributeTypeId = 0;
                }
            }
        }
    }
    return View();
}

这里,     如果我运行这意味着它将运行,我已选中复选框表示要启用的下拉列表。如果我从中选择任何值意味着它不返回其他值只返回第一个值。所以当我选择其他值时,我需要获取其他值。如何获取在HiddenField中Telerik的下拉列表中选择的文本???   任何帮助

0 个答案:

没有答案