简体   繁体   English

ASP.Net MVC Dropdownlist List selected item values not displayed in the same View

[英]ASP.Net MVC Dropdownlist List selected item values not displaying in the same View

I am trying to display a dropdown list in my View and I am able to generate the list items in the dropdown list and once an item is selected, I am trying to gather the details of the selected item and display it in the same View.我试图在我的视图中显示一个下拉列表,并且我能够在下拉列表中生成列表项,一旦选择了一个项目,我就会尝试收集所选项目的详细信息并将其显示在同一个视图中。

In my controller, I am getting a hit for the selected item and my jquery correctly redirects the item id to the ActionMethod and populating the model and returning the View, but the data is not getting displayed in the View.在我的控制器中,我得到了所选项目的命中,我的 jquery 正确地将项目 ID 重定向到 ActionMethod 并填充模型并返回视图,但数据没有显示在视图中。

My Index View:我的索引视图:

@model PWBMS.WebUI_1.Viewmodels.CustomerViewModel 

<div class="row">
    <h3 class="col-md-3">
        <i class="glyphicon glyphicon-user"></i>
        <strong>
            &nbsp;&nbsp;&nbsp;Customer&nbsp;
            <span id="cidd"> @Html.DropDownListFor(model => model.CustomerId, new SelectList(ViewBag.Customers, "CustomerId", "ShortName"), "Select Customers", htmlAttributes: new { @class = "form-control rtscustomtextboxmiddle", @id = "custId" })</span>
        </strong>
    </h3> 
</div>

  @if(Model != null)
  { 
     @Model.ShortName
  }

My jQuery to do the binding of the selected item in the dropdown list:我的 jQuery 对下拉列表中的选定项进行绑定:

@section scripts
{
    <script type="text/javascript">
        $(document).ready(function () {

            $("#cidd").change(function () {
                var y = $("#custId option:selected").val();
                $.ajax({
                    url: "@Url.Action("Index", "Customer3")",
                    method: "get",
                    async: "false",
                    data: { id: y }
                });
            });
        });
    </script> 
}

My Controller:我的控制器:

public ActionResult Index(int? id)
    {
        if(id is null)
        {
            ViewBag.Customers = db.Customers.OrderByDescending(o => o.CustomerId).ToList();
            return View();
        }
        var vm1 = db.Customers.SingleOrDefault(x => x.CustomerId == id);
        ViewBag.Customers = db.Customers.OrderByDescending(o => o.CustomerId).ToList();
        CustomerViewModel cvm = new CustomerViewModel
        {
            CustomerId = vm1.CustomerId,
            ShortName = vm1.ShortName
        };
        return View( cvm);
    }

The screenshot that are hitting the data:击中数据的屏幕截图: 控制器 ID 值 My second screenshot for model value我的模型值的第二个屏幕截图在此处输入图像描述

在此处输入图像描述

I hope this does not get closed for duplicate since i searched for a solution for my unique situation and I could not find.我希望这不会因为重复而关闭,因为我为我的独特情况寻找解决方案但我找不到。

I even tried placing the result set in a partial view, which is obtaining the correct result, but still not displaying it.我什至尝试将结果集放在局部视图中,它正在获得正确的结果,但仍然没有显示它。

I know i am doing something wrong, but not sure what I am doing wrong.我知道我做错了什么,但不确定我做错了什么。 Please help me point out my error.请帮我指出我的错误。 Thanks.谢谢。

Actually you are making an ajax request that you do not handle on view, there are two method to solve it, one simply post the form on change event or redirect to same view实际上,您正在发出一个您不在视图上处理的 ajax 请求,有两种方法可以解决它,一种只需在更改事件上发布表单或重定向到同一视图

document.location.href = '/controllerName/actionName/' + y;

If you really want to use ajax then update your code like below如果您真的想使用 ajax,请更新您的代码,如下所示

<div class="result"></div>

$.ajax({
    url: "@Url.Action("Index", "Customer3")",
    method: "get",
    async: "false",
    data: { id: y }
})
.done(function(r) {
    $('.result').html(r.ShortName);
})

you also need to update action您还需要更新操作

    if(id.HasValue)
    {
        var vm1 = db.Customers.SingleOrDefault(x => x.CustomerId == id.Value);
        ViewBag.Customers = db.Customers.OrderByDescending(o => o.CustomerId).ToList();
        CustomerViewModel cvm = new CustomerViewModel
        {
            CustomerId = vm1.CustomerId,
            ShortName = vm1.ShortName
        };      
        
        return Json(cvm, JsonRequestBehavior.AllowGet);
    }
    else
    {
        ViewBag.Customers = db.Customers.OrderByDescending(o => o.CustomerId).ToList();
        return View();      
    }

Please note that things related to ViewBag will not work while using the Ajax request, above code is just to give you idea, if you also want to update those values just create a ViewModel accordingly and return as json.请注意,在使用 Ajax 请求时,与 ViewBag 相关的内容将不起作用,上面的代码只是为了让您了解,如果您还想更新这些值,只需相应地创建一个 ViewModel 并以 json 形式返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM