skip to Main Content

I have method called ‘Upload’ that will receive two parameters (IformFile and a string value) how can i pass the selected item from my dropdown list to my controller which is the string parameter in my method.

<form enctype="multipart/form-data" method="post" asp-controller="Exposure" asp-action="Upload">
    <div class="row">
        <div class="form-group">
            <select class="form-control-sm" >
                <option selected>Choose a Database</option>
                <option value="SICS">SICS</option>
                <option value="TranslationTable">Translation Table</option>
            </select>
        </div>
    </div>
    <div class="row">
        <div class="col-6 mb-3">
            <div class="custom-file">
                <input type="file" class="custom-file-input" name="DBTemplate" />
                <label class="custom-file-label" for="customFile">Choose file</label>
                <i>Only Excel file .xls, .xlsx</i>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-6 ml-3">
            <div class="float-right">
                <button type="submit" id="submit" name="Submit" class="btn btn-info">Upload</button>
                <button asp-controller="Exposure" asp-action="Index" type="button" class="btn btn-light" data-dismiss="modal">Back to list</button>
            </div>
        </div>
    </div>
</form>


< --- Heres my Method --->
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Upload(IFormFile DBTemplate, string selectedItem)
        {
        
        }

2

Answers


  1. in your view:

    <select class="form-control-sm" name="somename >
    

    in your action:

     public IActionResult Upload(IFormFile DBTemplate,string somename)
            {
                .......
                return View();
            }
    
    Login or Signup to reply.
  2. Add name attribute to bind selectedItem,then you can pass selected item from dropdown list to the controller

    <select class="form-control-sm" name="selectedItem" >
                        <option selected>Choose a Database</option>
                        <option value="SICS">SICS</option>
                        <option value="TranslationTable">Translation Table</option>
                    </select>
    

    result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search