English 中文(简体)
Set selected value to @html.dropdownlistfor control
原标题:

My View code:

Unit @Html.DropDownListFor(m => m.Unit, UnitList, new { @class = "form-select" })

Gatepass Category @Html.DropDownListFor(m => m.GatePassCategory, GatepassCategoryList, new { @onchange = "CheckGatepassvaluevalid();", @class = "form-select" })

In My view code two dropdownlist "Area" and "GatepassCategory". In "Area" automatically set selected value on load. But in "GatepassCategory" dropdown it is not loaded.

My dropdownlist loaded from viewbag

Please help me on this problem. Thank you in advance.

问题回答

In My view code two dropdownlist "Area" and "GatepassCategory". In "Area" automatically set selected value on load. But in "GatepassCategory" dropdown it is not loaded.

Well, based on your shared code snippet and description it seems that your GatepassCategory dropdown value has not been loaded. There might be numerous reasons for that but according to your scenario, I cannot see how you are calling CheckGatepassvaluevalid method which is important.

Apart from that, you should call relevant controller to fetch your GatePassCategory data as well.

How to resolve:

Let s have a look how can we load the area dropdown and on its change event how can we load subsequent dropdown.

Demo Model:

public class GatePass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Area { get; set; }

    }

Load Area Controller Action:

public IActionResult LoadArea()
        {
            List<SelectListItem> areaList = new List<SelectListItem>();
            areaList.Add(new SelectListItem { Text = "Area-A", Value = "Area-A" });
            areaList.Add(new SelectListItem { Text = "Area-B", Value = "Area-B" });
            areaList.Add(new SelectListItem { Text = "Area-C", Value = "Area-C" });


            ViewBag.ListAreas = areaList;
            return View();
        }

Gate Pass Dropdown Value Load Controller Action:

        [HttpPost]
        public IActionResult CheckPassByArea(string areaId)
        {

            var gatePass = new List<GatePass>()
                 {
                  new GatePass {Id = 1,Name = "Gete-A", Area = "Area-A"},
                  new GatePass {Id = 2,Name = "Gete-B", Area = "Area-B"},
                  new GatePass {Id = 3,Name = "Gete-C", Area = "Area-C"},
                 };

            SelectList gatePassList = new SelectList(gatePass.Where(ar => ar.Area == areaId), "Id", "Name");
            return Json(gatePassList);
        }

Note: I am filtering gate pass by selected area in area dropdown.

View:

Area DropDown: @Html.DropDownList("Area", ViewBag.ListAreas, null, new { @class = "form-control", @onchange = "CheckGatePassValue(this.value)" })
<hr />
Gate Pass Dropdown: <select id="GatePass" name="GatePass" class="form-control"></select>

<script language="javascript" type="text/javascript">
    function CheckGatePassValue(areaId) {

        alert(areaId);
        var procemessage = "<option value= 0 > Please wait...</option>";
        $("#GatePass").html(procemessage).show();
        var url =  @Url.Action("CheckPassByArea", "ControllerName") ;

        $.ajax({
            url: url,
            data: { areaId: areaId },
            cache: false,
            type: "POST",
            success: function (data) {
                console.log(data);
                var markup = "<option value= 0 >Select Gate Pass</option>";
                for (var x = 0; x < data.length; x++) {
                    markup +=  <option value="  + data[x].value +  ">  + data[x].text +  </option> ;
                }
                $("#GatePass").html(markup).show();
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });

    }
</script>

Note: As you can see I am loading gate pass dropdown based on area name thus, I am calling CheckGatePassValue(this.value) with parameter. If you do not have any param you can just call it by CheckGatePassValue() with no parameter.

Output:

enter image description here

enter image description here





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签