locked
pulling a numerical value out of a dropdown box html RRS feed

  • Question

  • User-1694438709 posted

    Looking for some help on how to pull out a numerical value from a drop down box back into an if statement in a  controller:

    Dropdown box:

     <div class="form-group">
                <select class="form-control" name="Role">
                    <option value="">Select your accout type</option>
                    <option value="0">optionOne</option>
                    <option value="1">otionTwo</option>
                </select>
            </div>
         
     if (optionOne is chosen)
     {
           set a database field to 2                
     }
    
     if(optionTwo is chosen)                
     { 
           set a database field to 3
     }

    I'm looking at inserting two different integers  into a database depending on which option is selected from the dropdown box.


    Tuesday, April 14, 2020 1:59 PM

Answers

  • User-1694438709 posted

    Hey, thanks for the reply! I got waylay-ed with other stuff I'll take a look at it now!

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2020 12:02 PM

All replies

  • User475983607 posted

    Honestly, this is a very basic question that indicates you need to go through beginning level MVC tutorials before moving forward.  

    [HttpPost]
    public IActionResult Index(int? Role)
    {
        if (Role.HasValue)
        {
            User.Role = Role;
        }
                
        return View();
    }
    @{
        ViewData["Title"] = "Home Page";
    }
    
    <h1>Post Select Example</h1>
    
    <form method="post">
        <select class="form-control" name="Role">
            <option value="">Select your accout type</option>
            <option value="0">optionOne</option>
            <option value="1">otionTwo</option>
        </select>
        <inpu

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-3.1&tabs=visual-studio

    Tuesday, April 14, 2020 2:29 PM
  • User711641945 posted

    Hi binaary,

    It seems you want to know how to pass selected item's value to the controller,right?

    Here is a working demo:

    View:

    <form asp-action="Create">
        <div class="form-group">
            <select class="form-control" name="Role">
                <option value="">Select your accout type</option>
                <option value="0">optionOne</option>
                <option value="1">otionTwo</option>
            </select>
        </div>
        <div>
            <input type="submit" value="create" />
        </div>
    </form>

    Controller:

    [HttpPost]
    public void  Create(int Role)
    {
        var test = new Test();
        if(Role==0)  //optionOne's value
        {
           test.RoleId =2;
           //do your stuff...      
        }
        if(Role==1) //optionTwo's value
        {
           test.RoleId =3;
           //do your stuff...
        }
    }

    Result:

    Best Regards,

    Rena

    Wednesday, April 15, 2020 8:53 AM
  • User-1694438709 posted

    Hey, thanks for the reply! I got waylay-ed with other stuff I'll take a look at it now!

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2020 12:02 PM