Pass the selected value of a dropdown in to a URL.Action so that a modal form is displayed on a button click

I want to run up a modal form from a button click. The modal form will be depend on what is selected in the dropdown.

my code is –

  <div class="row">
      <div class="col-md-4 mb-2">
          <select id="AssessmentSelectionId" class="form-select no-progress" asp-for="@Model.AssessmentTypeId" asp-items="@(new SelectList(Model.AssessmentTypeList, "AssessmentTypeId", "AssessmentType"))">
              <option value="">Please select Assessment Type</option>
          </select>
      </div>
      <div class="col-md-4 mb-2">
          <button id="btnSaveAssessment" type="button" class="btn btn-primary float-right"
              data-toggle="ajax-modal" data-target="#mdIPDEAssessmentModal"
              data-url="@Url.Action("Create", "Assessments", new { assessmentType = *Pass the selected id*, assessmentId = 0})">Insert Assessment</button>
      </div>
  </div>

The code controller –

 [HttpGet]
 public IActionResult Create(int? assessmentType, int? assessmentId)
 {
     var modelData = new AssessmentsViewModel();

     if (assessmentType == 4)
     {
         return PartialView("_IPDEModalAssessment", modelData);
     }
     else
     {
         return PartialView("_GeneralModalAssessment", modelData);
     }
 }

So I basically want to pass $(‘#AssessmentSelectionId’).val() as the assessmentType in the url.action. it doesn’t like that Jquery if I type that in the action string.

Any help would be greatly appreciated.

[UPDATE]

Tried ajax call, see code below –

 $(function () {

     $("#btnSaveAssessment").click(function () {
         var assessemntTypeId = $('#AssessmentSelectionId').val();

         $.ajax({
             type: 'GET',
             url: '@Url.Action("Create", "Assessments")',
             data: { assessmentType: assessemntTypeId, assessmentId: 0 },
                     success: function (data) {
                       
                     },
                     error: function (xhr, status, error) {
                         console.error('Error fetching wards:', error);
                     }
                 });

         });
 });

This actions the appropriate action in the controller. But when the line –

return PartialView("_GeneralModalAssessment", modelData);

is actioned, nothing is shown.

The modal form is below –

@model AssessmentsViewModel

<div class="modal fade" id="mdGeneralAssessment" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Insert @Model.AssessmentType Results</h5>
            </div>

            <div class="modal-body">


            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-primary float-left" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" data-save="modal" id="btnSave">Save</button>
            </div>
        </div>
    </div>
</div>

There are no errors in the console.

Baffled!

3

Codes shall be simialr to below. Just like you see, we get selection value via jquery and append it behind the url. Then the callback function will get the view, we put the view in the DOM and use $('#partial_view_name').modal('show'); to show the modal. Because we are returning a partial view, then the modal content will be set into the DOM, we have to use execute the .modal('show') method to show the modal.

@model WebAppMvc.Controllers.Assessment

<div class="row">
    <div class="col-md-4 mb-2">
        <select class="form-select no-progress" asp-for="@Model.AssessmentTypeId" asp-items="@Model.AssessmentTypeList">
            <option value="">Please select Assessment Type</option>
        </select>
    </div>
    <div class="col-md-4 mb-2">
        <button id="btnSaveAssessment" type="button" class="btn btn-primary float-right"
                data-toggle="ajax-modal" data-target="#mdIPDEAssessmentModal"
                data-url="@Url.Action("Create", "Assessments")">
            Insert Assessment
        </button>
    </div>
</div>

<div id="popup"></div>

@section Scripts {
    <script>
        $('button[data-toggle="ajax-modal"]').click(function (event) {
            var url = $(this).data('url') + "?assessmentId=" + $("#AssessmentTypeId").val();
            $.get(url).done(function (data) {
                $("#popup").html(data);
                $('#homeNewsModal').modal('show');
            })
        })
    </script>
}

========================= — Update — ========================

In my partical view, I have view like below,

<div class="modal fade" id="homeNewsModal">
    <div class="modal-dialog">
        <div class="modal-content">
            content here
        </div>
    </div>
</div>

so that in the ajax success: function (data) {}, function, the data is the modal view. I need to put the modal view in a DOM, in my codes it’s $("#popup").html(data);. To make the modal view show up, I need to run $('#homeNewsModal').modal('show');. In your partical view, you have <div class="modal fade" id="mdGeneralAssessment", so that you should have callback function below. But you don’t have it.

success: function (data) {
    $("#your_dom_id").html(data);
    $('#mdGeneralAssessment').modal('show');                 
},

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật