0

I have a below model

public class category
{
    [Key]
    public long category_id { get; set; }
    public string category_desc { get; set; }
    public long? client_id { get; set; }
    public long? display_sno { get; set; }
}

controller passing the model to view

public ActionResult category(long? client_id)
{
    var category_type = db.category.Where(m => m.client_id == null).ToList();
    if(client_id == 10)
    {
        category_type = db.category.Where(m => m.client_id == client_id).ToList();
    }
    return View(category_type);
}

In view populating the radion button

@foreach (var item in Model)                                                 
{ 
    @Html.DisplayFor(modelItem => item.display_sno)<text>.</text> &nbsp;
    @Html.RadioButtonFor(modelItem => item.category_id, item.category_id, new { id=item.category_id})@item.category_desc
}

post Method

public ActionResult Category(category g)
{

}

In post method g is coming as null. What I am missing here?

Sachu
  • 7,555
  • 7
  • 55
  • 94
  • Your radio button does not make sense. You need a group of radio buttons bound to the same property with different values. Its no clear what your wanting to do with this code –  Nov 16 '17 at 05:23
  • @StephenMuecke category_id will be unique for each radiobutton and while posting back need to get the selected category_id – Sachu Nov 16 '17 at 05:25
  • Yes but for that your need a model with a property (lets say `public long category_id { get; set; }`) and the another property which is a collection containing the values for each radio button (lets say `public IEnumerable Values { get; set; }` and then its `foreach (var item in Model.Values { @Html.RadioButtonFor(m => m.category_id, item) }` –  Nov 16 '17 at 05:28
  • Which assuming `Values` is `[1,2,3,4]` would generate `` (and `value="2"` etc). And when you submit, if you selected the 2nd radio button, the the value of `category_id` would be `2` –  Nov 16 '17 at 05:30
  • @StephenMuecke values are not 1,2,3... category_id is random numbers.. – Sachu Nov 16 '17 at 05:31
  • I know :) - its just an example. You model needs a property to bind to and a property which is a collection of something that provides the values to use for the radio buttons. –  Nov 16 '17 at 05:32
  • @StephenMuecke i understood but how to get IEnumerable of category_id is confusing – Sachu Nov 16 '17 at 05:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159110/discussion-between-stephen-muecke-and-sachu). –  Nov 16 '17 at 05:36

1 Answers1

1

Your misunderstanding how radio buttons work. They are for binding one of many options to a single property, in the same way a dropdownlist works. Your code is generating radio buttons that bind to itself. And if you inspect the html you will see that it generates name="item.category_id" which would not bind to your model anyway (that would only bind to a model containing a complex object named Item which contained a property named category_id).

Assuming you have a model for a Product which contained a property for its Category, and you wanted to assign one of the values of your category table to the Category property, then your view models would look like

public class ProductVM
{
    [Required(ErrorMessage = "Please select a category")]
    public int? Category { get; set; }
    public IEnumerable<CategoryVM> CategoryOptions { get; set; }
}

public class CategoryVM
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Note the use of a nullable property for Category is explained in What does it mean for a property to be [Required] and nullable?

Then the controller methods (for a Create view) would look like

public ActionResult Create()
{
    ProductVM model = new ProductVM();
    ConfigureViewModel(model);
    return View(model);
}
public ActionResult Create(ProductVM model)
{
    if (!ModelState.IsValid())
    {
        ConfigureViewModel(model);
        return View(model);
    }
    .... // Initialize your Product data model, set its properties based on the view model
    // Save and redirect
}
private void ConfigureViewModel(ProductVM model)
{
    model.CategoryOptions = db.categories.Select(x => new CategoryVM
    {
        ID = x.category_id,
        Name = x.category_desc
    });
}

Then the view would be

@model ProductVM
....
@using (Html.BeginForm())
{
    @Html.DisplayNameFor(m => m.Category)
    foreach(var category in Model.CategoryOptions)
    {
        <label>
            @Html.RadioButtonFor(m => m.Category, category.ID, new { id = "" })
            <span>@category.Name</span>
        </label>
    }
    @Html.ValidationMessageFor(m => m.Category)
    <input type="submit" .... />
}

Note that the new { id = "" } is removing the id attribute which would otherwise be invalid html because of duplicates.