【问题标题】:Set the rendered value to the dropdown using ajax jquery使用ajax jquery将呈现的值设置为下拉列表
【发布时间】:2017-09-05 03:47:15
【问题描述】:

在文本框中获取用户名和电子邮件的值没有问题,但我在获取呈现值时遇到问题,即下拉列表中的角色名。 //控制器类 公共 JsonResult GetByID(int ID) { var user = userRepo.GetAllUsers().Find(x => x.UserID.Equals(ID)); 返回 Json(用户,JsonRequestBehavior.AllowGet); }

<script type="text/javascript">
function getByID(UserID)
{
    debugger;
    $('.uname').css('border-color', 'green');
    $('.mail').css('border-color', 'green');
    $.ajax({

        url: "../Easyshop/User/GetByID/" + UserID,
        type: "Get",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        success: function (result)
        {
            debugger;
            $('#Username').val(result.Username);
            $('#Email').val(result.Email);
            $('#roleName').val(result.RoleName);
        },
        error: function (errormessage)
        {
            alert(errormessage.responseText);
        }
    });
    return false;
}

result:Object
ConfirmPassword:null
Email:"bishal@gmail.com"
Password:null
RoleID:0
RoleName:"Member"
UserID:4
Username:"Bishal"
roleList:null

【问题讨论】:

  • 错误函数被触发了吗?
  • nope 错误函数没有被触发,因为它适用于文本框
  • 你有在 de DDL 中创建的值吗?
  • 是的。我确实从数据库中渲染了它
  • @Html.DropDownListFor(model =>model.RoleID, (IEnumerable)Model.roleList, new {@class= "form-control", @id="roleName"})

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


【解决方案1】:
  1. 对于单值,请使用以下语法

$("#roleName").empty();
$("#roleName").append('<option value="' + result.RoleName + '">' + "" + '</option>');
  1. 如果您必须加载值列表

 $("#roleName").empty();

 $("#roleName").append('<option value="' + 0 + '">' +"" + '</option>');

 $.each(data, function (i, RoleName) {

     $("#roleName").append('<option value="' + RoleName.Value + '">' +

     RoleName.Text + '</option>');
  });

【讨论】:

  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2020-01-24
相关资源
最近更新 更多