0

I have this radio button code in my MVC Razor View form and other form data.

<div class="row">
    <input type="radio" id="Apply-Self" name="selector">
    <label for="Apply-Self">Apply for Myself</label>
    <div class="check"></div>
    <input type="radio" id="Apply-Others" name="selector">
    <label for="Apply-Others">Apply for Others</label>
    <div class="check"><div class="inside"></div></div>
</div>

This radio button is not a datatype in my model because I'm using this as a front end option. When I click on submit my controller has all the data I want but want to use the radio button to do a condition check before I submit.

How can I get the radio button id to the controller so that I can do this?

[HttpPost]
public ActionResult ApplyAC(ACModel Model)
{
    if (Apply-Self is check)
    {
        var AddAC = Ac.Add(Model);//add data1 in model
    }
    else
    {
        var AddAC = Ac.Add(Model);//add a different data 
    }
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Wint
  • 39
  • 1
  • 10
  • 3
    I think you'd be better off using a view-model containing your ACModel and a property for your check box. See this question if you're unsure: http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc – Percy Nov 22 '16 at 08:22
  • And for a good example (do take the time to read it), see https://weblogs.asp.net/psheriff/bind-custom-radio-buttons-to-integer-data – Peter B Nov 22 '16 at 08:24
  • Is there another way round? Because this radio button value is not gonna be use in backend. of cos if there is no other ways then no choice have to do in model – Wint Nov 22 '16 at 08:25
  • 1
    But you are using it in the back end. You're making a decision based on it so it should be part of the model. – Trevor Cousins Nov 22 '16 at 08:47
  • @ZayleOng if the radio button isn't going to be used, why display it at all? Obviously it *is* used to display and/or capture some value that you want to use in the controller. You can work with a ViewBag but a ViewModel is cleaner – Panagiotis Kanavos Nov 22 '16 at 09:23
  • @ZayleOng are you confusing model objects with data objects? You *don't* have to use the same class for data retrieval and presentation. Quite often, it's a bad idea as a single view combines multiple data objects. – Panagiotis Kanavos Nov 22 '16 at 09:25

2 Answers2

0

OK, I found a solution. Simply add a catch by its selector name in the controller and values to the radio buttons.

<div class="row">
    <label for="Apply-Self"><input type="radio" id="Apply-Self" name="applyself" value="True" @(ApplySelf ? "checked='checked'" : "")>Apply for Myself</label>
    <div class="check"></div>

    <label for="Apply-Others"><input type="radio" id="Apply-Others" name="applyself" value="False" @(ApplySelf ? "":"checked='checked'") />Apply for Others</label>
    <div class="check"><div class="inside"></div></div>
</div>

Controller

[HttpPost]
public ActionResult ApplyAC(ACModel Model, bool selector)
{
    if (Selector)
    {
        var AddAC = Ac.Add(Model);//add data1 in model
    }
    else
    {
        var AddAC = Ac.Add(Model);//add a different data 
    }
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Wint
  • 39
  • 1
  • 10
  • 1
    This is not the recommended way. Here you are using the radio button as a checkbox. What if you need more than two state in the radio button? Instead you can bind the value to string (answer provided) (The best is to bind to an enum...) – apr Nov 22 '16 at 09:20
  • 1
    The *best* is to use a ViewModel. You've just broken the MVC pattern and perform view-related work in the controller. How hard is it to create a class with the properties you need? It's a lot simpler and safer than this code – Panagiotis Kanavos Nov 22 '16 at 09:26
0

You can bind it to a simple string value, on the server side. First add value attributes:

<div class="row">
    <input type="radio" id="Apply-Self" name="selector" value="self">
    <label for="Apply-Self">Apply for Myself</label>
    <div class="check"></div>
    <input type="radio" id="Apply-Others" name="selector" value="others">
    <label for="Apply-Others">Apply for Others</label>
    <div class="check"><div class="inside"></div></div>
</div>

On the server side you can bind the selected value to a string parameter:

[HttpPost]
public ActionResult ApplyAC(ACModel Model, string selector)
{
    if (selector == "self" )
    {
        // ...
    }
    else if( selector == "others" )
    {
        // ...
    }
}

The ideal solution would be to bind the value to an enum on the server side, but the string is easier.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
apr
  • 552
  • 3
  • 9