【问题标题】:posting the value from a DDL back to code behind将 DDL 中的值发布回代码后面
【发布时间】:2015-10-02 13:51:25
【问题描述】:

MVC 使用我在网上找到的一个示例,我填充了一个字典并将其传递给视图。我可以查看下拉列表并选择不同的值。但我的问题是如何在代码后面调用函数,并将选择的新值发回代码后面?

  //controller
     toolTipsVM.ListOfMaps = GetMapIds();

     public Dictionary<int, string> GetMapIds()
            {
                //List<int, string> mapIds = new List<int, string>();
                Dictionary<int, string> mapIds = new Dictionary<int, string>();
                mapIds.Add(36, "hi");
                mapIds.Add(37, "how");
                mapIds.Add(39, "now");

                return mapIds;
            }

    //VW
     public Dictionary<int, string> ListOfMaps { get; set; }

//View

     @Html.DropDownListFor(m => m.MapId, new SelectList(Model.ListOfMaps, "Key", "Value"),
                                            "--Choose Map--",
                                                new {@class = "form-control"}
                                        )

【问题讨论】:

  • MVC 中没有in code behind。你有表格吗?你想打一个ajax调用吗?你的 POST 方法是什么?
  • 创建一个基于逗号的数组并通过 c# 中的 ajax 请求将它们发送到控制器。这是从视图获取数据到控制器的最简单方法。

标签: c# asp.net-mvc-3 dictionary model-view-controller ddl


【解决方案1】:
  1. 你需要在Controller中编写函数来传递值。

    [Post]
    Post_MethodName(string Id)
    {
    }
    
  2. 使用以下 jQuery Ajax 调用将值发布到该函数。

    $.ajax({
         type: "POST",
         url: "Controller/Post_MethodName", // the method we are calling
         contentType: "application/json; charset=utf-8",
         data: {id: SelctedValue},
         dataType: "json",
         success: function (result) {
             alert('Success');
         ;                    
         },
         error: function (result) {
             alert('Failed');
         }
     });
    
  3. 使用以下函数获取下拉列表值

     $("#dropdownlistid option:selected").text();
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 2012-04-02
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2018-03-05
    相关资源
    最近更新 更多