1

Please help me. I need to get the value of the textbox and the value of the dropdown list.

So far i was able to get the value of the dropdown list using this code

public class DropdownOrder
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

public class DropdownOrderVM
    {
        public int DropdownId { get; set; }
        public string SearchValue { get; set; }
        public List<DropdownOrder> DropdownList { get; set; }
    }

public async Task<ActionResult> Index()
        {
            var vm = new DropdownOrderVM();
            vm.DropdownList = new List<DropdownOrder>
            {
                new DropdownOrder { Id = 1, Value = "1" },
                new DropdownOrder { Id = 2, Value = "2" },
                new DropdownOrder { Id = 3, Value = "3" }
            };


            return View(vm);
        }

And this is the view code

            <select class="form-control" asp-for="DropdownId"
                    asp-items="@(new SelectList(Model.DropdownList,"Id","Value"))">
                <option>Please select one</option>
            </select>
           
       
                <input type="search" name="search" id="search"  asp-for="SearchValue"  placeholder="Search" class="form-control" aria-label="Search">
                
          

and this is my controller which call called when the button submit is clicked

[HttpPost]
        public ActionResult Create(DropdownOrderVM model)
        {
            return View();
        }

I can only get the value of the dropdown. But i added the code in the view

<input type="search" name="search" id="search"  asp-for="SearchValue" >

So why does my code doesn't return the user input in the textbox?

What did i do wrong?

2
  • Can you post your whole view pls, including model part Commented Jul 7, 2021 at 17:48
  • You can delete the code name="search",or you can change to name=SearchValue,it will work. Commented Jul 8, 2021 at 1:35

1 Answer 1

2

You are explicitly giving name & id to your text box which doesn't match the model field. Remove these 3 attributes from your search textbox: type="search" name="search" id="search". Tag helper asp-for will do this for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.