Question

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.

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top