简体   繁体   English

Asp.Net MVC从Dropdownlist获取价值到PartialView

[英]Asp.Net MVC Get value from Dropdownlist to PartialView

In my main page I have Dropdownlist of Cars. 在我的主页面中,我有汽车下拉列表。 And it works fine. 它工作正常。
And When I select one car from Dropdownlist I want to get back all car modells who belongs to the selected Car from Dropdownlist in the same view, let say under Dropdownlist. 当我从Dropdownlist中选择一辆车时,我想在同一视图中从Dropdownlist中取回属于所选车辆的所有车型,请在Dropdownlist下面说。 DropDownlist with Cars and Models of the selected cars in the same View (in Main view). DropDownlist与汽车和同一视图中所选汽车的型号(在主视图中)。 I tried with PartialView but I'am not so good when it comes PartielView and html code This is my action to get CarModels , and I think this must be as PartialView 我尝试使用PartialView,但是当它来到PartielView和html代码时我不是很好这是我获取CarModels动作,我认为这必须是PartialView

[HttpGet]
public ActionResult GetCarModel(int? carId)
{
    List<CarModelVW> listOfCarModel;

    using (Db db = new Db())
    {
        listOfCarModel = db.CarModel.ToArray()
                        .Where(x => carId == null || carId == 0 || x.carId == carId)
                        .Select(x => new CarModelVW(x))
                        .ToList();

    }
    return View(listOfCarModel);
}

In my Main View with DropDownlist 在DropDownlist的主视图中

<div class="form-group">
    <label class="control-label col-md-2" for="HasSidebar"> Car </label>
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.CarId, Model.Cars, "---Select car---", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.CarId, "", new { @class = "text-danger" })
    </div>
</div>

And here below Dropdownlist I want to get all carmodels of selected car But I think I have to create <div> </div> with Id or span .. but I'am not sure how to do here. 在下面的Dropdownlist中我想获得所有车型的所有汽车模型但是我认为我必须用Id或span创建<div> </div>但我不知道该怎么做。

And here is javascript I'am trying to use 这是javascript我试图使用

$("#CarId").change(function () {        
    var carId = $(this).val();
    if (carId) {
        window.location = "/an/GetCarModel?carId=" + carId;
    }
    return false; 
});

How do I get this action GetCarModel below Dropdownlist view? 如何在Dropdownlist视图下面获取此操作GetCarModel? Thank you in advance. 先感谢您。

If you want to view results on the same page then you need to use Ajax. 如果要在同一页面上查看结果,则需要使用Ajax。

add this div tag Main view where you want to display results: 添加此div标签您要显示结果的主视图:

<div id="carmodels"></div>

Change your jquery function as below: 更改您的jquery函数如下:

$("#CarId").change(function () {
  var carId = $(this).val();
  if (carId) 
    {
        $.get("/an/GetCarModel?carId=" + carId, function(data){
        $("#carmodels").html(data);
        });
   }
});

Just like Roman said, instead of navigating to the url, you should send an ajax call to the control. 就像Roman说的那样,你应该向控件发送一个ajax调用,而不是导航到url。

First of all, add an id to your dropdownlist. 首先,在您的下拉列表中添加一个ID。

Secondly, in the Onchange function add an ajax call to get cars: 其次,在Onchange函数中添加一个ajax调用来获取汽车:

var dropdownval = $("#//Id of dropdown").val();
var json = '{Id: ' + dropdownval + '}';

$.ajax({
    url:'@Url.Action("GetCarModel", "//The controller")',
    type:'POST',
    data: json,
    contentType:'Application/json',
    success:function(result){
        //Do whatever
    }
})

You can then do whatever with the result you pass back to the ajax call 然后,您可以对传递回ajax调用的结果执行任何操作

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

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