如何使用LINQ在下拉列表中设置所选项?

时间:2017-04-05 08:55:49

标签: c# asp.net-mvc linq

目前我的代码正在选择列表中的第一项。我希望它选择与MakeModelTypeCode匹配的项目。

E.G。 下拉列表中的所选项目应该是此代码的位置

 vehicleViewModel.VehicleDTO.VehicleDetails.MakeModelTypeCode
来自MakeModelTypeCode

= x

以下是Business Logic类的相关代码:

        vehicleViewModel.AvailableMakeModels = GetAllMakeModelTypesForClient(selectedClientId);

        vehicleViewModel.AvailableMakeModels = vehicleViewModel.AvailableMakeModels.GroupBy(x => x.ModelDescription).Select(x => x.First()).Distinct().ToList();

        var vehicleMakeList = vehicleViewModel.AvailableMakeModels
            .Select(s =>
                new SelectListItem
                {
                    Selected = true,
                    Text = s.MakeDescription,
                    Value = s.MakeModelTypeCode
                });

以下是.cshtml中的相关代码:

        <div class="col-sm-6">
            @Html.LabelFor(m => m.VehicleDTO.VehicleDetails.MakeDescription)
            @Html.DropDownListFor(x => x.SelectedvendorText, new SelectList(Model.AvailableMakesSelectList, "Value", "Text", "Make"), new { @class = "form-control uppercase", @id = "ddlAvailableMakes", name = "ddlAvailableMakes", @onchange = "FillModels()" })
            @Html.ValidationMessageFor(m => m.SelectedMake, "", new { @class = "text-danger" })
        </div>

以下是Controller的代码:

    [Route("Edit-{id}")]
    [Authorize]
    public ActionResult Edit(string id)
    {
        VehicleViewModel vehicleViewModel = new VehicleViewModel();
        selectedClientId = HelperMethods.GetClientId();

        vehicleViewModel.VehicleDTO = this.vehicleBusinessLogic.GetClientVehicleDTO(id, this.selectedClientId);

        vehicleViewModel = this.vehicleBusinessLogic.SetUpUpdateVehicle(vehicleViewModel, selectedClientId);
        vehicleViewModel.VehicleDTO.VehicleDetails.ClientID = this.selectedClientId;

        return View(vehicleViewModel);
    }

我尝试了几种不同的方法但却无法工作。我只能获得我想要的项目,但不能获得所有项目+所选项目。

我以为我可以在Select中做一个嵌套的Where子句,但这似乎不起作用,但也许我的语法不正确。

2 个答案:

答案 0 :(得分:0)

怎么样

MakeModelTypeCode toBeSelected;

var vehicleMakeList = vehicleViewModel.AvailableMakeModels
            .Select(s =>
                new SelectListItem
                {
                    Selected = s.MakeModelTypeCode == toBeSelected,
                    Text = s.MakeDescription,
                    Value = s.MakeModelTypeCode
                });

答案 1 :(得分:0)

将.cshtml更改为以下解决了该问题。

唯一的变化是将s3.listObjects(params, function (err, data) { if (err) console.log(err, err.stack); // an error occurred var lastZipfile = null; var lastModified = null; data.Contents.forEach(function (c) { if (c.Key.endsWith('tar.gz')) { if (lastModified === null) { lastZipfile = c.Key; lastModified = c.LastModified; } else { // Compare the last modified dates if (lastModified <= c.LastModified) { // Track the new latest file lastZipfile = c.Key; lastModified = c.LastModified; //extractData(lastZipfile); } } } }); }); 替换为x => x.SelectedvendorText

m => m.VehicleDTO.VehicleDetails.MakeModelTypeCode