Access Radio Buttons inside Table in Controller in ASP.Net MVC

Last Reply one month ago By dharmendr

Posted one month ago

hi everyone.

I am newbie to MVC 

I have created a small feedback form which contains a set of questions where each question have five opinions (e.g. Very Good, Good, Average, Bad, VeryBad).

I have desined this in Html Table, Now I am in trouble I dont have idea how to create a modal for that and also how to insert records inside database..

please give some explanatory solution so that I can achieve this task and understand the concepts.

Please give solution in MVC...

<table>
    <tr>
        <td>3.</td>
        <td style="text-align: left">Question 1</td>
        <td class="cell-center">
            <input id="Athree" name="Athree" type="radio" value="StronglyAgree" />
        </td>
        <td class="cell-center">
            <input id="Radio49" name="Athree" type="radio" value="Agree" />
        </td>
        <td class="cell-center">
            <input id="Radio50" name="Athree" type="radio" value="Neutral" />
        </td>
        <td class="cell-center">
            <input id="Radio51" name="Athree" type="radio" value="Disagree" />
        </td>
        <td class="cell-center">
            <input id="Radio52" name="Athree" type="radio" value="StronglyDisagree" />
        </td>
    </tr>
    <tr>
        <td>4.</td>
        <td style="text-align: left">Question 2  </td>
        <td class="cell-center">
            <input id="Afour" name="Afour" type="radio" value="StronglyAgree" />
        </td>
        <td class="cell-center">
            <input id="Radio53" name="Afour" type="radio" value="Agree" />
        </td>
        <td class="cell-center">
            <input id="Radio54" name="Afour" type="radio" value="Neutral" />
        </td>
        <td class="cell-center">
            <input id="Radio55" name="Afour" type="radio" value="Disagree" />
        </td>
        <td class="cell-center">
            <input id="Radio56" name="Afour" type="radio" value="StronglyDisagree" />
        </td>
    </tr>
</table>

Thanks In Advance..

Posted one month ago
Hi @Prasunjeet,
Please try the following

How to save record RadioButton inside ListView in to database in ASP.Net

It might help you.

Cheers Andrea.

Posted one month ago

Thanks for the reply,

But its not helpful for me a bit..

I am using Razor View in MVC4 and Have created a table like as follows 

 

 

 

<table>
<tr>
                                    <td>3.</td>
                                    <td style="text-align: left">Question 1</td>
                                    <td class="cell-center">
                                        <input id="Athree" name="Athree" type="radio" value="StronglyAgree" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio49" name="Athree" type="radio" value="Agree" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio50" name="Athree" type="radio" value="Neutral" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio51" name="Athree" type="radio" value="Disagree" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio52" name="Athree" type="radio" value="StronglyDisagree" />
                                    </td>
                                </tr>
<tr>
                                    <td>4.</td>
                                    <td style="text-align: left">Question 2  </td>
                                    <td class="cell-center">
                                        <input id="Afour" name="Afour" type="radio" value="StronglyAgree" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio53" name="Afour" type="radio" value="Agree" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio54" name="Afour" type="radio" value="Neutral" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio55" name="Afour" type="radio" value="Disagree" />
                                    </td>
                                    <td class="cell-center">
                                        <input id="Radio56" name="Afour" type="radio" value="StronglyDisagree" />
                                    </td>
                                  </tr>
</table>

Now I want to write code to store this question and selected answer to database .. so how is this posssible in MVC4 with Razor View


Posted one month ago

Hi Prasunjeet,

You can access the RadioButton Group value using the FormCollection.

Check this example. Now please take its reference and correct your code.

View

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Save(FormCollection collection)
    {
        string athree = collection["Athree"];
        string afour = collection["Afour"];
        return View("Index");
    }
}

View

<table>
    <tr>
        <td>3.</td>
        <td style="text-align: left">Question 1</td>
        <td class="cell-center">
            <input id="Athree" name="Athree" type="radio" value="StronglyAgree" />StronglyAgree
        </td>
        <td class="cell-center">
            <input id="Radio49" name="Athree" type="radio" value="Agree" />Agree
        </td>
        <td class="cell-center">
            <input id="Radio50" name="Athree" type="radio" value="Neutral" />Neutral
        </td>
        <td class="cell-center">
            <input id="Radio51" name="Athree" type="radio" value="Disagree" />Disagree
        </td>
        <td class="cell-center">
            <input id="Radio52" name="Athree" type="radio" value="StronglyDisagree" />StronglyDisagree
        </td>
    </tr>
    <tr>
        <td>4.</td>
        <td style="text-align: left">Question 2</td>
        <td class="cell-center">
            <input id="Afour" name="Afour" type="radio" value="StronglyAgree" />StronglyAgree
        </td>
        <td class="cell-center">
            <input id="Radio53" name="Afour" type="radio" value="Agree" />Agree
        </td>
        <td class="cell-center">
            <input id="Radio54" name="Afour" type="radio" value="Neutral" />Neutral
        </td>
        <td class="cell-center">
            <input id="Radio55" name="Afour" type="radio" value="Disagree" />Disagree
        </td>
        <td class="cell-center">
            <input id="Radio56" name="Afour" type="radio" value="StronglyDisagree" />StronglyDisagree
        </td>
    </tr>
</table>
<br />
<input type="submit" value="Save" />

Screenshot