1

I have a MVC3 Razor form. It have a radiobutton list and some another text fields. When I press submit controller post action get the view model, which have all fields seted correctly, except RegionID.

Model:

    namespace SSHS.Models.RecorderModels
    {
        public class CreateViewModel
        {
             ...
             public int RegionID { get; set; }
             ...
        }
    }

Controller:

    namespace SSHS.Controllers
    {
        public class RecorderController : Controller
        {
            ...
            public ActionResult Create()
            {
                EntrantDBEntities db = new EntrantDBEntities();

                List Regions = new List(db.Region); 
                List Schools = new List(db.School);
                List Settlements = new List(db.settlement);

                CreateViewModel newEntr = new CreateViewModel();

                ViewBag.Regions = Regions;
                ViewBag.Schools = Schools;
                ViewBag.Settlements = Settlements;

                return View(newEntr);
            }

            [HttpPost]
            public ActionResult Create(CreateViewModel m)
            {
                EntrantDBEntities db = new EntrantDBEntities();

                Entrant e = new Entrant()
                {
                    FatherName = m.FatherName,
                    Lastname = m.LastName,
                    LocalAddress = m.LocalAddress,
                    Name = m.Name,
                    RegionID = m.RegionID,
                    PassportID = m.PassportID,
                    SchoolID = m.SchoolID,
                    SettlementID = m.SattlementID,
                    TaxID = m.TaxID,
                };

                db.Entrant.AddObject(e);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
    }

View:

    @model SSHS.Models.RecorderModels.CreateViewModel
    @using SSHS.Models 

    @using (Html.BeginForm("Create", "Recorder", FormMethod.Post))
    {       
    
    
        @foreach (Region item in ViewBag.Regions)
        {
           
           @Html.RadioButtonFor(m => m.RegionID, item.RegionID)
           @Html.Label(item.RegionName) - @item.RegionID
           
        }
    
          ...
          ...    
    
    }

The Create(CreateViewModel m) method gets data from all textboxes normaly, but RegionID always is 0.

webdeveloper
  • 17,174
  • 3
  • 48
  • 47
RazorRunner
  • 131
  • 14
  • Radio button is used for true/false state. According your case you might have used Select List. Try to check Request["YourRegionRadioId"] in [HttpPost] Create method, if it has any value. –  Sep 20 '12 at 08:21
  • Request["RegionSelect"] not set to an instance of an object – RazorRunner Sep 20 '12 at 08:34
  • This should work. I suspect that the code you are showing us is somehow not your actual code. – Darin Dimitrov Sep 20 '12 at 09:42

1 Answers1

1

How are you planning to fill radio button with int ? It have two states: checked and not. Could you tell us, what are you trying to do? Make radio group? Use bool for RadioButtonFor.

Added:

You need to write something like this: CheckboxList in MVC3.0 (in your example you will have radio buttons)

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47