Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下拉列表不更新所选值_C#_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

C# 下拉列表不更新所选值

C# 下拉列表不更新所选值,c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,我有一个下拉列表: 所有数据通过此Ajax请求发送到控制器: $.ajax({ url: "/DeviceUsage/Edit", type: "POST", contentType: "application/json; charset=utf-8", headers: { 'RequestVerificationToken': '@TokenHeaderValue()' }, data: JSON.stringify({

我有一个下拉列表:

所有数据通过此Ajax请求发送到控制器:

    $.ajax({
    url: "/DeviceUsage/Edit",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    headers: {
        'RequestVerificationToken': '@TokenHeaderValue()'
    },
    data: JSON.stringify({
        deviceusage: {
            DeviceInstanceId: $('.a').children("#DeviceInstanceId").val(),
            UserId: $('.a').children('#UserId').val(),
            StorageId: $('.a').children('#storageId').val()

        }
    }),
    error: function (data) {
        alert("wystąpił nieokreślony błąd " + data);
    },
    success: function (data) {
        if (data.ok) {
            $("#Modal").modal('hide');
            window.location = data.newurl;
        }
        else {
            $('.modal-body').html(data);
       }
    }
})
无论我在这个下拉列表中选择什么,它都不会更新。更改第一个选择后,始终将第一个选择发送到控制器

@更新: 以下是我用于处理“Post”调用的控制器方法:

public ActionResult Edit([Bind(Include="StorageId,UserId,DeviceInstanceId")] DeviceUsage deviceusage)
    {
        ValidateRequestHeader(Request);
        if (deviceusage.UserId == 6 && deviceusage.StorageId == (int)Storage.Biurko)
        {
            ModelState.AddModelError("", "Zarezerwowane urządzenie nie moze byc przypisane do biurka");
        }
        if (deviceusage.UserId == 1 && deviceusage.StorageId == (int)Storage.Biurko)
        {
            ModelState.AddModelError("", "Wolne urządzenie nie może przebywać na jakimś biurku");
        }
        if ((deviceusage.UserId != 1 & deviceusage.UserId != 6) & deviceusage.StorageId != (int)Storage.Biurko)
        {
            ModelState.AddModelError("", "Urzązenie przypisane do kogos nie moze przebywac w magazynie");
        }
        if (ModelState.IsValid)
        {

            unitOfWork.deviceUsageRepository.Update(deviceusage);
            unitOfWork.Save();
            return Json(new { ok = true, newurl = Url.Action("Index") });
        }
        ViewBag.DeviceInstanceId = new SelectList(unitOfWork.deviceInstanceRepository.Get(), "Id", "SerialNo", deviceusage.DeviceInstanceId);
        ViewBag.StorageId = new SelectList(unitOfWork.storageRepository.Get(), "Id", "Name", deviceusage.StorageId);
        var data = unitOfWork.userRepository.Get()
        .Select(s => new
        {
            Id = s.Id,
            Credentials = s.Name + " " + s.Surname
        }
        );
        ViewBag.UserId = new SelectList(data, "Id", "Credentials", deviceusage.UserId);
        return PartialView(deviceusage);
    }
正如您所看到的,它返回一个PartialView,因为下拉列表位于模态窗口中,该窗口会随着Ajax调用的返回而更新

@更新2

测试期间,使用浏览器控制台执行以下代码:

$('#StorageId').val()
我设法发现:

它会在第一次发送之前正确返回值

如果由于该原因重新加载模态,则数据错误。使用列表更改选定值不会更改任何内容。此代码返回的值是falue send with ajax

尝试:

注意StorageId上的大写字母“S”

你也可以这样做:

StorageId: $('#StorageId :selected').val();

我创建了一个JSFIDLE来演示:

始终第一个值被提交到控制器的问题背后的原因是因为这个属性在您的下拉列表框中设置为第一个选项,selected=selected

您可以像这样通过更改事件回调绕过此行为

JQUERY代码:

在$document.ready函数{……}或页面主体的onload中添加上述事件侦听器


快乐编码:

您能发布生成的下拉列表框的html dom结构吗?抱歉,还是什么都没有。我更新了问题,添加了其他内容data@szpic:您能通知$'.a'.children'StorageId'.val;在ajax调用的数据块中,此时效果如下:出现模态。我从列表中选择一些值并发送它。警报显示eq 23,由于某种原因,在控制器中检查其错误值。模态窗口用新的html更新。我正在更改eq 1下拉列表中的选定项,此警报显示23,而不是1try$'.a StorageId'.val;并警告此值以验证其选择是否正确made@well很明显,当重新加载模式数据时,下拉列表正在损坏。检查ajax调用返回的数据是否正确构造了html是值得的
public ActionResult Edit([Bind(Include="StorageId,UserId,DeviceInstanceId")] DeviceUsage deviceusage)
    {
        ValidateRequestHeader(Request);
        if (deviceusage.UserId == 6 && deviceusage.StorageId == (int)Storage.Biurko)
        {
            ModelState.AddModelError("", "Zarezerwowane urządzenie nie moze byc przypisane do biurka");
        }
        if (deviceusage.UserId == 1 && deviceusage.StorageId == (int)Storage.Biurko)
        {
            ModelState.AddModelError("", "Wolne urządzenie nie może przebywać na jakimś biurku");
        }
        if ((deviceusage.UserId != 1 & deviceusage.UserId != 6) & deviceusage.StorageId != (int)Storage.Biurko)
        {
            ModelState.AddModelError("", "Urzązenie przypisane do kogos nie moze przebywac w magazynie");
        }
        if (ModelState.IsValid)
        {

            unitOfWork.deviceUsageRepository.Update(deviceusage);
            unitOfWork.Save();
            return Json(new { ok = true, newurl = Url.Action("Index") });
        }
        ViewBag.DeviceInstanceId = new SelectList(unitOfWork.deviceInstanceRepository.Get(), "Id", "SerialNo", deviceusage.DeviceInstanceId);
        ViewBag.StorageId = new SelectList(unitOfWork.storageRepository.Get(), "Id", "Name", deviceusage.StorageId);
        var data = unitOfWork.userRepository.Get()
        .Select(s => new
        {
            Id = s.Id,
            Credentials = s.Name + " " + s.Surname
        }
        );
        ViewBag.UserId = new SelectList(data, "Id", "Credentials", deviceusage.UserId);
        return PartialView(deviceusage);
    }
$('#StorageId').val()
StorageId: $('.a').children('#StorageId').val()
StorageId: $('#StorageId :selected').val();
$('.a select').on('change',function() {
      $(this).find('option:selected').attr("selected","selected");
});