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 class="payment-body pl-3 pr-3">
<div class="pay-option-btns py-4">
<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 class="px-3 rounded border-0 pay-with-transfer mt-2 mt-lg-0" 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" class="mr-2">
<path d="M11.5 10V17H7V10H4Z" fill="white" />
</svg>
<span class="text-capitalize text-white">Pay with Gamma</span>
</button>
<a href="@Url.Action("MakePayment", "Payment", new {email = Model.Email, amount = Model.Total, reference = Model.ProposalNumber, RecurringPayment = Model.RecurringPayment } )" class="px-3 rounded border-0 d-flex align-items-center pay-with-transfer mt-2 mt-lg-0" style="font-size: 12px">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="mr-2">
<path d="6.4127Z" fill="white" />
</svg>
<span class="text-capitalize text-white" 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);
}
2
Answers
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:Now, you can read the value from the current Request using Request.Form:
Alternatively, you need to send the
Model
to yourController
instead of sending it a as astring
:Did you try this?
model => model.RecurringPayment
instead ofModel => Model.RecurringPayment
.