How to read a chosen value of HTML.RadioButtonFor in HTTPPOST?

2.7k Views Asked by At

I've read a lot of answers at SO, however it is not fit to me.

I have model:

class SomeOrder
{
     public string TypeDrink {get;  set;}
}

Controller:

public ViewResult Edit(int id)
{
  SomeOrder se = newSomeOrder{ TypeDrink=3 };
  return View(se);
}

And view:

@Html.EditorForModel   @Html.RadioButtonFor(m=>m.TypeDrink, "1") Tea
@Html.RadioButtonFor(m=>m.TypeDrink, "2") Coffee  
@Html.RadioButtonFor(m=>m.TypeDrink, "3") Juice

How to read a chosen value of radiobutton in a [HTTPPOST] method? In my HTTPPOST method the preselected value is stored, not the chosen by user:

[HTTPPOST]
public ViewResult Edit(SomeOrder se) 
 {
   string chosenValue=se.TypeDrink;// always the old selected value
 }
3

There are 3 best solutions below

0
On BEST ANSWER

Your view includes @Html.EditorForModel() before your radio buttons. EditorForModel() will generate form controls for each property in your model so it will be generating a control for property TypeDrink. Depending on attributes applied to your property, and any EditorTemplates you may have, it may generated in a hidden input.

Because your form then posts back the name/pair values of the input generated by EditorForModel first, the value of the input will be bound to your model and the value of the radio buttons will be ignored by the DefaultModelBinder.

Remove the EditorForModel from your view and the model will be bound based on value of the radio buttons.

1
On

if your post action is like

[HttPost]
public ViewResult Edit(SomeOrder model)
{
  // should get it like
  model.TypeDrink;
}

this is utilizing the model binding in mvc.

you could also look at the Request.Form["TypeDrink"] to get the value, but it is not recomended

0
On

Your Model is:

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;

     namespace radiotest.Models
     {
        public class SomeOrder
        {
            public string TypeDrink { get; set; }
        }
     }

Your view is index.cshtml

    @model radiotest.Models.SomeOrder

     @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
      }
     @using (Html.BeginForm())
     {
        <fieldset>
            <div class="editor-field">
                @Html.RadioButtonFor(m => m.TypeDrink, "1") Tea
                @Html.RadioButtonFor(m => m.TypeDrink, "2") Coffee
                @Html.RadioButtonFor(m => m.TypeDrink, "3") Juice
            </div>
            <div> <input type="submit" value="Submit" /></div>
        </fieldset>

     }

Your Controller in HTTP Get and Post is:

    using radiotest.Models;
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
     using System.Web.Mvc;

     namespace radiotest.Controllers
     {
        public class TestController : Controller
        {

            public ActionResu`enter code here`lt Index()
            {
                 SomeOrder se = new SomeOrder{ TypeDrink="3" };
                 return View(se);
            }

            [HttpPost]
            public ActionResult Index(SomeOrder model)
            {
                //model.TypeDrink gives you selected radio button in HTTP POST
                SomeOrder se = new SomeOrder {TypeDrink = model.TypeDrink };
                return View(se);
            }
        }
     }