Home > other >  How to pass selected bootstrap dropdown value to the controller in ASP.NET MVC
How to pass selected bootstrap dropdown value to the controller in ASP.NET MVC

Time:03-14

I inherited this code base in which I need to pass extra property to the controller. This field happens to be a select dropdown for users to select yes or no. I need to pass that value to the controller.

The new property I am adding is RecurringPayment, I have already added the property to the model, but I am unable to pass the selected Yes or No to the controller. I am getting null in the controller

I have tried the code below with the bootstrap dropdown.

<!-- This is the select drop down -->
<div >
    <div >
        <select asp-for="" id="RecurringPayment" 
                name="RecurringPayment" asp-items="@Model.RecurringPayment">
            <option value="">Recurring payment?</option>
            <option value="Yes">Yes</option>
            <option value="No">No</option>
        </select>

        <!-- This is the inherited code -->
        <button  type="button" style="font-size: 12px" onclick="callGamma()">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" >
                <path d="M11.5  10V17H7V10H4Z" fill="white" />
            </svg>
            <span >Pay with Gamma</span>
        </button>
        <a href="@Url.Action("MakePayment", "Payment", new {email = Model.Email, amount = Model.Total, reference = Model.ProposalNumber, RecurringPayment = Model.RecurringPayment } )"  style="font-size: 12px">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" >
                <path d="6.4127Z" fill="white" />
            </svg>
            <span  data-bs-toggle="tooltip" data-bs-placement="top" title="Please note that your subsequent premium will be charged automatically base on your policy">pay with paystack</span>
        </a>
    </div>
</div>

I have also tried using DropDownListFor

@Html.DropDownListFor(Model => Model.RecurringPayment,
                      new List<SelectListItem> {
                            new SelectListItem { Value="", Text = "Do you want recurring changes" },
                            new SelectListItem { Value="Yes", Text = "Yes" },
                            new SelectListItem { Value="No", Text = "No" }
                      },
                      new { @class = "myselect" })

But I got an error:

CS0136: A local variable named 'Model' cannot be declared in this scope because it would give a different meaning to 'Model', which is already used in a 'parent or current' scope to denote something else

Controller method:

public ActionResult MakePayment(string email, double amount, string reference, string RecurringPayment)
{
    PaystackRequest request = new PaystackRequest()
                                  { 
                                      amount = amount, 
                                      email = email, 
                                      reference = reference, 
                                      RecurringPayment = RecurringPayment 
                                  };

    var result = apicall.Payment(request);

    return Redirect(result);
}

CodePudding user response:

You can make the following changes to get your selected value from the drop down list in your Controller method:

First assign an id attribute to your drop down list:

@Html.DropDownListFor(m => Model.RecurringPayment,
                      new List<SelectListItem> {
                            new SelectListItem { Value="", Text = "Do you want recurring changes" },
                            new SelectListItem { Value="Yes", Text = "Yes" },
                            new SelectListItem { Value="No", Text = "No" }
                      },
                      new { @class = "myselect", @id = "ddlPayment" })

Now, you can read the value from the current Request using Request.Form:

public ActionResult MakePayment(string email, double amount, string reference)
{
    string selectedDDLValue= Convert.ToString(Request.Form["ddlPayment"]);
    PaystackRequest request = new PaystackRequest()
                                  { 
                                      amount = amount, 
                                      email = email, 
                                      reference = reference, 
                                      RecurringPayment = RecurringPayment 
                                  };

    var result = apicall.Payment(request);

    return Redirect(result);
}

Alternatively, you need to send the Model to your Controller instead of sending it a as a string:

public ActionResult MakePayment(string email, double amount, string reference, RecurringPayment recurringpayment)

CodePudding user response:

Did you try this?

@Html.DropDownListFor(model => model.RecurringPayment,
                      new List<SelectListItem> {
                            new SelectListItem { Value="", Text = "Do you want recurring changes" },
                            new SelectListItem { Value="Yes", Text = "Yes" },
                            new SelectListItem { Value="No", Text = "No" }
                      },
                      new { @class = "myselect" })

model => model.RecurringPayment instead of Model => Model.RecurringPayment.

  • Related