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

Last Reply 6 months ago By dharmendr

Posted 6 months 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..

You are viewing reply posted by: dharmendr 6 months ago.
Posted 6 months 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