- Home
- Forum
- ASP.NET Core - EJ 2
- Readig the seleceted checkbox value and saving it into a database column
Readig the seleceted checkbox value and saving it into a database column
Dear Syncfusion support,
How can I initialize two different radio buttons (Yes and No) and then read the value of a selected button, pass it to the controller and then save the selected value into the database. I am saving the value in a field called "Diarrhea" in my table.
Below is my inititialization of the radio butons in my razor view.
<ejs-radiobutton id="Yess" label="Yes" value="4" name="Diarrhea" checked="false"></ejs-radiobutton>
<br /><br />
<ejs-radiobutton id="Noo" label="No" value="5" name="Diarrhea" checked="false"></ejs-radiobutton>
SIGN IN To post a reply.
5 Replies
1 reply marked as answer
MV
Madhan Venkateshan
Syncfusion Team
July 23, 2020 11:39 AM UTC
Hi Ssekamatte James,
Good day to you.
You can use ‘getSelectedValue’ method of radio button control to get the selected value and use this value to save it in the database. Please refer the below code snippets.
|
<ejs-radiobutton id="Yess" label="Yes" value="4" name="Diarrhea" checked="false"></ejs-radiobutton>
<br />
<br />
<ejs-radiobutton id="Noo" label="No" value="5" name="Diarrhea" checked="false"></ejs-radiobutton>
<br />
<br />
<button id="getValue" class="e-btn">Get Selected Value</button>
<script>
document.getElementById("getValue").addEventListener("click", () => {
var radiobuttonObj = ej.base.getComponent(document.getElementById("Yess"), "radio");
var selectedValue = radiobuttonObj.getSelectedValue();
// post the value here to pass it to the controller to save it to the database
})
</script> |
Regards,
Madhan V
SJ
Ssekamatte James
July 23, 2020 12:10 PM UTC
Hello Madhan, Thank you for your response. Howevr, your provided solution is not worked for me. According to you, you have used a button you called Get Selected Value to return the value of the selected button instead of getting that selected value using the default dialog form Save button because this is what I am using.
Attachment: RadioButton_c7e5808e.rar
I have the radio buttons defined in the _DialogAddPartial.cshtml form that is loaded from the MonitoringTable.cshtml on editing a particular record (if (args.requestType === 'beginEdit'). So I want that when I click on save from the dialog form in the _DialogAddPartial.cshtml form, the selected radio button value should be submitted to the controller under the Diarrhea field.
I have attached the _DialogAddPartial.cshtml View, MonitoringTable.cshtml view and the controller. The radio buttons are defined on line 49 of the _DialogAddPartial.cshtml View.
The save method in my controller is called the DialogInsert method.
Thank You
Attachment: RadioButton_c7e5808e.rar
MV
Madhan Venkateshan
Syncfusion Team
July 24, 2020 12:08 PM UTC
Hi Ssekamatte James,
Good day to you.
You can achieve this by using model binding with radio button by adding ‘Diarrhea’ string type property with already existing model, so it will show the selected value inside the model (model.Diarrhea) in controller method when you submit form. And also you can achieve this by adding additional argument to controller method with name of the radio button with string type. Please refer the below code snippets and sample link.
_RadioButtonView.cshtml
|
@model WebApplication1.Controllers.RadioButtonModel
<h6>Using Model</h6>
<ejs-radiobutton id="Yess" label="Yes" value="4" ejs-for="Diarrhea"></ejs-radiobutton>
<br />
<br />
<ejs-radiobutton id="Noo" label="No" value="5" ejs-for="Diarrhea"></ejs-radiobutton>
<br />
<h6>Without Using Model</h6>
<ejs-radiobutton id="Yess2" label="Yes" value="4" name="Diarrhea2"></ejs-radiobutton>
<br />
<br />
<ejs-radiobutton id="Noo2" label="No" value="5" name="Diarrhea2"></ejs-radiobutton> |
HomeController.cs
|
[HttpPost]
public IActionResult Index(RadioButtonModel model, string Diarrhea2)
{
return View();
}
public class RadioButtonModel
{ // Add this along with your model
public string Diarrhea { get; set; }
} |
Sample link: https://www.syncfusion.com/downloads/support/forum/156275/ze/PartialViewRadiobuttonFormSubmit63165019
Regards,
Madhan V
Marked as answer
SJ
Ssekamatte James
July 29, 2020 07:28 AM UTC
Thank You.This worked .
MV
Madhan Venkateshan
Syncfusion Team
July 30, 2020 06:01 AM UTC
Hi Ssekamatte James,
Most welcome.
Regards,
Madhan V
SIGN IN To post a reply.
- 5 Replies
- 2 Participants
- Marked answer
-
SJ Ssekamatte James
- Jul 22, 2020 11:45 AM UTC
- Jul 30, 2020 06:01 AM UTC