0

I'm using MVC.

I have this method inside my Home (index page) controller

public IActionResult Requestservice()
    {
        return View();
    }

There is a page dedicated to each service the website is offering. When the client clicks a button called "Request service" in the "Barbecue" page,

The expected behaviour would be:

  • Open the form page where the client inputs contact/ residence information
  • Have the "Barbecue package" pre-selected from a dropdown list where they can select the kind of service they want.

Instead, the current behaviour is:

  • Client clicks "Request service" button on the Barbecue page.
  • On the form page, the selected option is the first item written on the <ul> code block in html.

HTML snippet on the RequestService.cshtml page

(...)

<select class="form-control" id="exampleFormControlSelect1">
    <option>Pack Complete (€34.99/pax)</option>
    <option>Pack Barbecue (€9.99/pax)</option>
</select>
(...)

This is what the button looks like on the Barbecue.cshtml page

<a class="btn btn-primary" href="RequestService">
     Request Service
     <span class="glyphicon glyphicon-chevron-right"></span>
</a>

So, again, when I click the button Request Service on the barbecue page, how can I make it so that the default selected is "Pack Barbecue" and not "Pack Complete"?

enter image description here

C. Rib
  • 340
  • 3
  • 19

3 Answers3

0

Try this code.

<select class="form-control" id="exampleFormControlSelect1">
    <option>Pack Complete (€34.99/pax)</option>
    <option seleted>Pack Barbecue (€9.99/pax)</option>
</select>

Note, I add selected for "Pack Barbecure" option to make it selected on first load.

Mohsin Mehmood
  • 4,156
  • 2
  • 12
  • 18
  • Thanks, but that's not what I was going for. That will make the pre-selected option "Pack barbecue" **regardless** of context – C. Rib May 02 '18 at 19:32
0

I think you can do this:

<a class="btn btn-primary" href="RequestService?option=barbecue">


public IActionResult Requestservice(string option)
    {
        ViewBag.DefaultMenuSelection = option;
        return View();
    }

<select class="form-control" id="exampleFormControlSelect1">
    <option @(ViewBag.DefaultMenuSelection == "packcomplete" ? "selected" : String.Empty)>Pack Complete (€34.99/pax)</option>
    <option @(ViewBag.DefaultMenuSelection == "barbecue" ? "selected" : String.Empty)>Pack Barbecue (€9.99/pax)</option>
</select>
Mohsin Mehmood
  • 4,156
  • 2
  • 12
  • 18
  • At first this seems like it culd work but the compiler says I can't have c# code isnide a declaration area, is there any way around this? – C. Rib May 02 '18 at 20:27
  • 1
    Try this: – Mohsin Mehmood May 02 '18 at 20:35
  • It doesn't work, because with String.Empty it becomes – C. Rib May 02 '18 at 21:05
  • You should have a model and bind to a model (all this is done automatically out of the box) –  May 02 '18 at 21:54
0

I did it!

Here's my method in HomeController:

public IActionResult Requisitar(string option)
    {
        ViewBag.DefaultMenuSelection = null;

        if (option != null) {
            ViewBag.DefaultMenuSelection = option;
        }

        return View();
    }

And here's my html code based on Mohsin's answer:

<select class="form-control" id="packDropdown">
       <option>Pack Complete (€34.99/pax)</option>
       <option selected="@(ViewBag.DefaultMenuSelection == "Pack_barbecue" ? "selected" : null)">Pack Barbecue (€9.99/pax)</option>
</select>

Then on the request button I have this:

<a class="btn btn-primary"  href="@Url.Action("Request_service", null, new { area = String.Empty, controller = "Home", option="Pack_barbecue"})">
                Request Service
                <span class="glyphicon glyphicon-chevron-right"></span>
</a>

That's it!

C. Rib
  • 340
  • 3
  • 19
  • 1
    Suggest you read [MVC5 - How to set “selectedValue” in DropDownListFor Html helper](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper/41731685#41731685) to understand how to use MVC correctly –  May 02 '18 at 21:58