0

On this form to create a link when I select another committee type the form validates for all of the blank fields that are required. I am trying to figure out a way so those fields don't validate when I select another committee type in the dropdown. Currently when I debug it doesn't get to the post action method in the controller for obvious reasons that the form validates before even getting into the controller.

If you need to see more code like the action method in controller or view model I can provide that as well.

View

    <div class="form-group col-md-8">
       <div asp-validation-summary="All" id="validation-error" class="text-danger custom-validation- 
        summary"></div>
     </div>

     <input id="link-id" asp-for="@Model.LinkId" type="hidden" />
     <input name="FetchCategories" type="hidden"/>
    <div class="form-group col-md-8 col-lg-4">
      <div class="form-group">
           @{
        var authorizedCommitteeTypes = await Model.CommitteeType
            .ToSelectListAsync(AuthorizationService, User, AuthRequirements.AdminCommitteeType);

        if (authorizedCommitteeTypes.Count == 1)
        {
            <input id="committeeType" name="committeeType" type="hidden" 
            value="@authorizedCommitteeTypes.FirstOrDefault()?.Value" />
        }
        else
        {
            <label class="control-label">Committee Type</label>
            <select id="add-edit-committee-type"
                    name="committeeType"
                    asp-for="@Model.CommitteeType"
                    asp-items="@authorizedCommitteeTypes"
                    class="form-control">
            </select>
        }
    }
       </div>
  </div>
    <div class="form-group col-md-8 col-lg-4">
     <label class="control-label">Category</label>
   @{
    if (Model != null && Model.AvailableCategories != null)
       {
         var availableCategories =
            new SelectList(
                Model.AvailableCategories.OrderBy(c => c.Order),
                dataValueField: "CategoryId",
                dataTextField: "Title",
                selectedValue: Model.CategoryId);

        <select id="dropdown-linkCategories" required
                asp-for="@Model.CategoryId"
                asp-items="@availableCategories"
                class="form-control">
            <option>-- Select --</option>
        </select>
       }
      else
       {
        <select id="dropdown-linkCategories"
                class="form-control">
            <option>-- Select --</option>
        </select>
        }
      }
     </div>


   <div class="form-group col-md-8 col-lg-4">
    <label class="control-label">Title</label>
  <input id="title" asp-for="Title" name="Title" class="form-control" />
 </div>

 <div class="form-group col-md-8 col-lg-4">
    <label class="control-label">Display Order</label>
   <div>
     <input id="order" asp-for="Order" name="Order" class="form-control" />
     </div>
  </div>

   <div class="form-group col-md-8 col-lg-4">
    <label class="control-label">URL</label>
     <input id="url" asp-for="URL" name="URL" class="form-control" />
 </div>

 <div class="form-group col-md-8 col-lg-12">
   <label class="control-label">Description</label>
     <textarea class="rtextDescription" name="Description" id="Description" row="1" cols="60"
          data-val-maxlength-max="200" asp-for="Description"
          data-val-maxlength="Max length for Description is 200"></textarea>
   </div>

  @{

    if (Model.LinkId == 0)
    {
        <div class="form-group col-md-12">
            <input type="submit" id="link-submit"
                   class="btn btn-forum col-sm-12 col-md-2 col-lg-2"
                   value="Add" />
            <a asp-area="Admin"
               asp-controller="Link"
               asp-action="Index"
               class="btn btn-forum col-sm-12 col-md-2 col-lg-2">Back to Links</a>
        </div>
    }
    else
    {
        <div class="form-group col-md-8 col-lg-12">
            <input type="submit" value="Save" id="edit-submit"
                   class="btn btn-forum col-sm-12 col-md-2 col-lg-2" />

            <a asp-area="Admin"
                asp-controller="Link"
               asp-action="Index"
               class="btn btn-forum col-sm-12 col-md-2 col-lg-2">Back to Links</a>
         </div>
      }
  }

JS

$(document).on("change", "#form-create-link #add-edit-committee-type", function () {
$('input[name="FetchCategories"]').val(true);  
$(this).closest('form').submit()
});
bootsy
  • 87
  • 1
  • 3
  • 14
  • Does this one help you? https://stackoverflow.com/questions/18111915/remove-required-property-from-input-field-on-form-submit – Doan Van Thang Feb 13 '21 at 21:13
  • Why do you submit the form when change the select option? – mj1313 Feb 15 '21 at 03:50
  • @Doan Van Thang. I don't think this is what I'm looking for. I need to get the categories when I change the selection in the committee type dropdown. I realize I need to remove the submit but after doing that and then I debug it doesn't even get into the controller action method at all. I'm missing something in the jquery. That's what I need help with. – bootsy Feb 15 '21 at 06:38
  • Well, you removed `.submit()` so it won't get into the controller is normal. I can only guess that `input[name="FetchCategories"]` out of `
    `. I suggest you should remove type hidden and add `console.log("something")` into your js when debugging
    – Doan Van Thang Feb 15 '21 at 07:06
  • Anyone else with any other ideas for this. It would be appreciated. – bootsy Feb 16 '21 at 03:37

0 Answers0