【问题标题】:Pass parameter from View to controller MVC 4将参数从视图传递到控制器 MVC 4
【发布时间】:2016-04-02 16:12:51
【问题描述】:


我想将下拉列表的选定值从视图传递到 MVC 4 中 html.BeginForm 中的控制器。
我可以传递查询字符串的值,但我不知道如何传递下拉列表的选定值。
欢迎所有建议。
这是我附上的代码

<form>
    <fieldset class="form-group" id="ddl2">
        <label for="exampleSelect1">Section Type:</label>
            @(Html.Kendo().DropDownList()
                .Name("ddlsection")
                .DataTextField("Name")
                .DataValueField("Id")
                .OptionLabel("--Select--")
                .DataSource(source =>
                 {
                    source.Read(read =>
                    {
                        read.Action("GetSectionType", "LookUp");
                    });
                })
                .Events(e => e.Change("onChange_ddlsection"))
                .HtmlAttributes(new { @class = "form-control" })
            )
    </fieldset>
</form>
    <div class="WordClass">
        @using (Html.BeginForm("GetConditionListingInWord", "Inspection", new { sectionId = 'What should be here?' }, FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <input id="Submit1" type="submit" value=""  class="WordClassA tooltipSource" title='Print List in MS-Word format'  data-placement='bottom' data-toggle='tooltip' />
        }
    </div>

我想在“这里应该有什么?”中选择“ddlsection”下拉菜单的值部分。

【问题讨论】:

  • 首先,您的Kendo().DropDownList() 需要在表单标签内。不确定new { sectionId = 'What should be here?' } 是什么意思? sectionId 是你要绑定的值吗?
  • 感谢您的快速回复。是的,sectionId 是参数名称,我想用这个绑定下拉列表的选定值。
  • 删除new { sectionId = 'What should be here?' }并将您的下拉列表放在&lt;form&gt;标签内并将其命名为sectionId
  • 我必须在控制器中传递 7 个下拉列表和 2 个文本字段的值。以为我必须将所有这些都放在
    标记中?
  • 如果您想将值发布到您的控制器方法,那么可以(但使用模型,而不是 POST 方法中的 9 个参数)

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


【解决方案1】:

您可以使用 Ajax 调用将 Kendo Dropdownlist 的选定值传递给 Controller,然后根据需要重定向到另一个 Action,如下所示:

<script type="text/javascript">

    $(function () {

        //Returns Dropdownlist's selected values to the Controller
        $("#btnSubmit").click(function (e) {
            e.preventDefault();

            var data = {
                ProjectID: $('#ProjectID').val(), //parameter I
                IssueID: $('#IssueID').val()      //parameter II
            }
            $.ajax({
                type: "POST",
                url: "/Controller/Action",
                cache: false,
                data: JSON.stringify(data),
                dataType: this.dataType,
                contentType: "application/json; charset=utf-8",                 
                success: function (data) {
                    // Variable data contains the data you get from the action method
                    window.location = "/Controller/OtherAction/" + data
                },
                error: function (data) {
                    $("#error_message").html(data);
                }
            }); 
        });

    });

</script>

希望这会有所帮助...

【讨论】:

    【解决方案2】:
    For that you have to make AJAX call to your controler method
    
    var userModel = {
                 RoleId: $("#drpRoleId").data("kendoDropDownList").value(),
            }
    
    $.ajax({
                url: '@Url.Action("AddEditUser","User")',
                type: 'POST',
                data: JSON.stringify(userModel),
                async: true,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
               }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多