0

I would like to set radio buttons checked base on results from query such as in my case its a medical questionnaire. A medical questionnaire results last quite a while.

So if user do not have a valid medical questionnaire, the use will be directed to a empty questionnaire page to select the yes or no radio buttons. Part of the coding of the questionnaire looks like this:

<tr>
<td width="33">1.</td>
<td width="491">Heart or circulatory problems including: high blood pressure, heart attack, angina, heart murmur, heart failure, palpitations, circulatory problemseg. whitefinger, blocked arteries, stroke aneurysm.</td>
<td width="68"><input name="medq1" id="yes" type="radio" value="yes" onclick="displayTextBox()" /><label for="yes"> Yes </label></td>
<td width="78"><input name="medq1" id="no" type="radio" value="no" onclick="displayTextBox()"/><label for="no"> No </label></td>

There are 20 over question but i am not going to post all as it will be confusing.

However if the user have a valid medical questionnaire stored in the database,when he apply for the permit, he will be redirected to the same questionnaire page, but instead of it being empty, the radio button of yes and no should be checked base on the result from the SQL query.

var sql = "SELECT * FROM Medical WHERE CDSID = @0";
var MedicalResult = db.QuerySingle(sql,myCDSID);
var myQ1 = MedicalResult.Q1;
:
:
:
var myQ20 = MedicalResult.Q20;

So now the results are stored in variables and we can use the variables as the result to tell the radio button which must be checked.

However, I am noob beginner in Razor C# and I only know very very minimum but still in the learning process. I know the codes in PHP but not in Razor C#

In php, pretend we have the results from the sql already.

<input <?php if ($myQ1 == 'yes'){ echo 'checked="checked"'; } else { } ?> name="medq1" id="yes" type="radio" value="yes" onclick="displayTextBox()" /><label for="yes"> Yes </label>
<input <?php if ($myQ1 == 'no'){ echo 'checked="checked"'; } else { } ?> name="medq1" id="no" type="radio" value="no" onclick="displayTextBox()"/><label for="no"> No </label>

But how do I do this in Razor C#??

Thanks in advance for guidance. Still in learning process.

Panda
  • 71
  • 1
  • 2
  • 9

1 Answers1

0

You can do it similarly using the ViewBag and @Html.Raw() helper method.

// Controller
public ActionResult YourPage()
{
    ViewBag.MedQ1 = // bool value from SQL
    View();
}

In YourPage.cshtml

<input @Html.Raw(ViewBag.MedQ1? "checked": "") name="medq1" id="yes" type="radio" value="yes" onclick="displayTextBox()" /><label for="yes"> Yes </label>
<input @Html.Raw(ViewBag.MedQ1? "": "checked") name="medq1" id="no" type="radio" value="no" onclick="displayTextBox()"/><label for="no"> No </label>
Thach Mai
  • 915
  • 1
  • 6
  • 16
  • Note that this should only be used for isolated cases. If you find yourself repeating the code, you should definitely take a look at [partial view](http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/) – Thach Mai Apr 21 '12 at 01:12
  • do i put the controller in the @{...} ? is ViewBag a type of function? – Panda Apr 21 '12 at 01:15
  • @Panda The ViewBag is a dynamic object "shared" by you controller and the view. The controller is in the Controllers folder of the project and is where your code for calling the "pages" are (the `Actions`). This is an `Action` and should be inside the `Controller` for your page. Checkout: http://www.asp.net/mvc/overview/controllers-and-routing – Ricardo Souza Apr 21 '12 at 01:38
  • Panda, do you know how to create a controller and a view? If you do not, then I suggest that you should start with that first... – Thach Mai Apr 21 '12 at 23:59