【问题标题】:How i can catch the value text of dropdownlist in asp MVC?我如何在 asp MVC 中捕获下拉列表的值文本?
【发布时间】:2016-08-12 20:46:37
【问题描述】:

我试图让 webgrid 可以用户编辑和删除它内联所以我把它做成 webgrid 并且我成功绑定它并且更新功能将起作用并且当我按下编辑按钮标签转换为文本和下拉列表有我的ajax代码

  <script type="text/javascript">
$(function () {
    $('.edit-mode').hide();
    $('.edit-user, .cancel-user').on('click', function () {
        var tr = $(this).parents('tr:first');


        tr.find('.edit-mode, .display-mode').toggle();
    });
    $('.save-user').on('click', function () {
        var getValue;
        var tr = $(this).parents('tr:first');
        var FirstName = tr.find("#FirstName").val();
        var Phone = tr.find("#Phone").val();
        var Branch = tr.find("#Branch-drop").val();

        getValue = $("#Branch-drop option:selected").text();

        var UserID = tr.find("#UserID").html();
        tr.find("#lblFirstName").text(FirstName);
        tr.find("#lblPhone").text(Phone);
        tr.find("#lblBracnh").text(getValue);
        tr.find('.edit-mode, .display-mode').toggle();
        var UserModel = {
            "IdEmp": UserID,
            "EmpName": FirstName,
            "Phone": Phone,
            "BranchName": Branch
        };
        $.ajax({
            url: '/Home/UpdateUser/',
            data: JSON.stringify(UserModel),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data);

            }
        });
    });
})




  </script>

webgrid 代码是

           @{
        var grid = new WebGrid(Model);
         }


     <div id="gridContent" style=" padding:20px; ">
      @grid.GetHtml(
    tableStyle: "webgrid-table",
    headerStyle: "webgrid-header",
    footerStyle: "webgrid-footer",
    alternatingRowStyle: "webgrid-alternating-row",
    selectedRowStyle: "webgrid-selected-row",
    rowStyle: "webgrid-row-style",
    mode: WebGridPagerModes.All,
    columns:
        grid.Columns(
             grid.Column("IdEmp", "ID", format: @<text>  <span class="display-mode">@item.IdEmp </span> <label id="UserID" class="edit-mode">@item.IdEmp</label> </text>, style: "col1Width" ),
         grid.Column("EmpName","Employee Name", format: @<text>  <span class="display-mode"> <label id="lblFirstName">@item.EmpName</label> </span> <input type="text" id="FirstName" value="@item.EmpName" class="edit-mode" /></text>, style: "col2Width"),
        grid.Column("Phone", "Phone", format: @<text> <span class="display-mode"> <label id="lblPhone">@item.Phone</label> </span>  <input type="text" id="Phone" value="@item.Phone" class="edit-mode" /> </text>, style: "col2Width"),
      grid.Column("BranchName","Branches", format: @<text><span class="display-mode"> <label id="lblBracnh">@item.BranchName</label> </span>   @Html.DropDownList("MyDropdowen", (SelectList)ViewBag.DropDowenValue, new { id = "Branch-drop", @class = "edit-mode" })</text>,style:"col2Width"),
         grid.Column("Action", format: @<text>
    <button class="edit-user display-mode">Edit</button>
    <button class="save-user edit-mode">Save</button>
    <button class="cancel-user edit-mode">Cancel</button>
    <button class="Delete-user ">Delete</button>
        </text>, style: "col3Width", canSort: false)
                         ))

问题是下拉列表出现时未显示在当前值上,当我更改文本时,标签不采用新值所以我如何使下拉列表采用当前值以及用户何时选择新文本并按保存下拉菜单获取新值?

【问题讨论】:

  • 您可以验证您的“ViewBag.DropDowenValue”是否为空
  • 不它不是空的它返回列表

标签: c# jquery ajax asp.net-mvc webgrid


【解决方案1】:

下拉列表的获取值文本语法错误 这是我的代码:

  getValue = $("#Branch-drop option:selected").text();

应该是:

      getValue = tr.find("#Branch-drop option:selected").text();  

并且成功了 谢谢大家

【讨论】:

    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多