按专业创建用户 - 如何填充/提交/验证表单?

时间:2010-09-07 20:40:04

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

我正在尝试通过注册表单创建用户,该表单包含ASP.NET MVC中的专业下拉列表

EG。个人,专业,经理等。

一些领域很常见,如名称和姓氏......

某些领域的职业是独一无二的......

我如何用ajax编程。

由于

1 个答案:

答案 0 :(得分:0)

我通过在控制器中创建SelectList并将其作为视图模型的一部分传递给视图来完成此类操作。然后在视图中,我可以选择在SelectedValue更改时执行某些操作,或者只是在其他内容触发对控制器的调用时返回下拉列表的值。

Controller code:
            int count = 0;
            List<string> YearList = new List<string>();
            for (int i = 2000; i < (DateTime.Now.Year + 6); i += 4)
            {
                YearList.Add(i.ToString());
                if (i < iyear)
                    count++;
            }
            var q = from c in doc.Descendants("candidate")
                    select new can_sw_model
                    {
                        name = c.Attribute("name").Value,
                        office = c.Parent.Attribute("name").Value.ToUpper(),
                        party = c.Attribute("party").Value,
                    };
            can_sw_view model = new can_sw_view()
            {
                YearList = new SelectList(YearList),
                value = YearList[count],
                model = q,
            };
            return View(model);

View code:
    <script type="text/javascript">
        $(document).ready(function() {
            $('#YearList').val('<%= Model.value %>');
            $('#YearList').change(function(event) {
                window.location.replace('<%= ResolveUrl("~/Candidate/sw_candidates") %>' + "?year=" + $('#YearList').val());
            });
        });
        function pdfclick() {
            var grid = $("#grid1").data("tGrid");
            window.location.replace('<%= ResolveUrl("~/Candidate/pdf") %>' + "?year=" + $('#YearList').val() + "&tab=statewide" +
                "&page=" + grid.currentPage + "&orderBy=" + grid.orderBy + "&groupBy=" + grid.groupBy + "&filterBy=" + grid.filterBy);
        }
    </script>

我希望这有帮助! 鲍勃