pass the selected value in a drop down to the controller

elsvieta 371 Reputation points
2024-01-31T14:56:30.48+00:00

hi all,

I am having difficulties in understanding how to pass the selected value in a drop down list to the controller.

I have only seen examples that use the model object and the selected value is just some attribute that is being referenced when creating the instance of the page with the model object argument.

My page(s) are already detail page(s) and they are being instantiated using the ID of the element as argument; my sequence is as follows:

  1. I open a page (GetItemID) with a form in it
  2. I type an id
  3. click a button (submit)
  4. open a new page (ItemDetails) with the details of the item and a dropdown list and a submit button
  5. select a value in the dropdown
  6. click the submit button
  7. open a third page (ItemDetailsAndValueSelected)

The code is as follows:

        // GET: GetItemID
        [HttpGet]
        public ActionResult GetItemID()
        {
            return View();
            
        }

        // POST: GetItemID
        [HttpPost]
        public ActionResult GetItemID(string ITEM_ID)
        {
            var item = context.Item.FirstOrDefault(m => m.ITEM_ID == ITEM_ID);
            
            return RedirectToAction("ItemDetails", new {ITEM_ID = item.ITEM_ID });
        }

        // GET: ItemDetails
        [HttpGet]
        public ActionResult 
        {
            var item = _context.Item.FirstOrDefault(m => m.ITEM_ID == ITEM_ID);
           
            return View(item);
        }

        // POST: ItemDetails
        [HttpPost]
        public ActionResult ItemDetails(string ARIBA_INVOICE_ID, string ddPick)
        {
            var item = _context.Item.FirstOrDefault(m => m.ITEM_ID == ITEM_ID);
            
            return RedirectToAction("ItemDetailsAndValueSelected", new { ITEM_ID = item.ITEM_ID });
        }

The Post for ItemDetails is not well written and I got nothing yet for the ItemDetailsAndValueSelected, yet.

Like I said, the only examples I've seen are using Item item and the action is like below

public ActionResult SomeAction(Item myItem)
{
    if (ModelState.IsValid)
    {
      var selected=  myItem.dropDownList; //selected value of DDList
.....
   }
}

All my actions are like:

public ActionResult SomeAction(string myID)
{    
	var item = _context.Item.FirstOrDefault(m => m.ITEM_ID == ITEM_ID);

	return View(item); 
}

The main idea is to be able to pass the initial details for the id typed on the first form and the value selected in the second form into the third form.

Thanks,

elsvieta

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,724 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,555 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 29,121 Reputation points
    2024-01-31T16:54:53.7366667+00:00

    I think the following is what you are looking for. Keep in mind, this is sample code. You'll need to populate the dropdown with values from your data. I removed the redirect with a form that does a an HTTP GET to ItemDetails. The post to ItemDetails returns JSON so you can see the values passed to the action.

        public class ItemController : Controller
        {
    
            [HttpGet]
            public ActionResult GetItemID()
            {
                return View();
            }
    
    
            [HttpGet]
            public ActionResult ItemDetails(int ITEM_ID)
            {
                var item = new Item() { ITEM_ID = ITEM_ID };
    
    
                List<SelectListItem> options = new List<SelectListItem>()
                {
                    new SelectListItem() 
                    {
                        Value = "1", 
                        Text="Option 1" 
                    },
                    new SelectListItem()
                    {
                        Value = "2",
                        Text="Option 2"
                    },
                    new SelectListItem()
                    {
                        Value = "3",
                        Text="Option 3"
                    }
                };
    
                ViewData["Options"] = options;
    
                return View(item);
            }
    
            // POST: ItemDetails
            [HttpPost]
            public ActionResult ItemDetails(string ITEM_ID, string ddPick)
            {
                return Ok(new { ITEM_ID = ITEM_ID, ddPick = ddPick });
           
    

    GetItemID.cshtml

    
    @{
        ViewData["Title"] = "GetItemID";
    }
    
    <h1>GetItemID</h1>
    <div>
        <form asp-action="ItemDetails" method="get">
            <div>
                <input type="text" name="ITEM_ID" />
            </div>
            <div>
                <input type="submit" value="submit"
            </div>
        </form>
    </div>
    
    

    ItemDetails.cshtml

    @model MvcDemo.Models.Item
    @{
        ViewData["Title"] = "ItemDetails";
    }
    
    <h1>ItemDetails</h1>
    
    <div>
        <form method="post">
            <div>
                <input type="hidden" asp-for="ITEM_ID" />
                <label>@Model.ITEM_ID</label>
            </div>
            <div>
                <select name="ddPick" asp-items="@((List<SelectListItem>)ViewData["Options"])">
                    <option value="">--Select--</option>
                </select>
            </div>
            <div>
                <input type="submit" value="submit" />
            </div>
        </form>
    </div>
    

    Item Model

        public class Item
        {
            public int ITEM_ID { get; set; }
        }
    
    
    

    This site has a lot of great tutorials that cover fundamental programming patterns in MVC. I recommend the going through the following. Get started with ASP.NET Core MVC ASP.NET Core MVC with EF Core - tutorial series

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.