简体   繁体   中英

populate radiobuttonlist from database table in asp.net mvc

在此输入图像描述

I want to create a form like above,

also have to populate Viewer User Groups accodrding to UserGroups table 在此输入图像描述

public partial class UserGroup
{
    public string UserGroup_ID { get; set; }
    public string UserGroupNameEn { get; set; }
}

So populate this user groups and get the filled data I created new model (so it can apply to view (1st image))

I followed theses Stack overflow question 1 and question 2 to make this

this is controller class for this

    [HttpGet]
    public ActionResult ProductFields_Create()
    {
        var model = new ProductFields
        {
            LisGroups = GetUserGroupListEn()
        };

        return View(model);

    }

    public MultiSelectList GetUserGroupListEn()
    {
        return new MultiSelectList(db.UserGroup.ToList(), "UserGroup_ID", "UserGroupNameEn");
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ProductFields_Create(ProductFields product_fields)
    {
      ...........
    }

this is model class for first image

public class ProductFields
{
    public string ProductFieldID { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { get; set; }
    public string ProdcutFieldDiscriptionEn { get; set; }
    public string ProductFieldDiscriptionAr { get; set; }

    public List<UserGroup> LisGroups { get; set; }

    [Required]
    public string SelectedGroupID { get; set; }
}

viewpage for above view

@model Project_Name.Models.ProductFields

@using (Html.BeginForm())
{

    <fieldset>
         <div class="form-horizontal">           

            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

            .....

             <div class="form-group">
             <div>
                 @Html.ValidationMessageFor(x => x.SelectedGroupID)
                 foreach (var group in Model.LisGroups)
                 {
                 <div>
                     @Html.RadioButtonFor(x => x.SelectedGroupID, group.UserGroup_ID, new { id = "emp" + group.UserGroup_ID })
                     @Html.Label("emp" + group.UserGroup_ID, employee.Label)
                 </div>
                 }

             </div>

          </div>

           <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
           </div>


        </div>
    </fieldset>
}

but here I'm getting major errors with red squiggly lines

The name 'group' does not exist in the current context

在此输入图像描述

Cannot implicitly convert type 'System.Web.Mvc.MultiSelectList' to 'System.Collections.Generic.List'

在此输入图像描述

You do not need a MultiSelectList (which is a class for use in the ListBox() and ListBoxFor() methods). And the reason for the error is that your property is List<UserGroup> LisGroups but your attempting to assign typeof MultiSelectList to it.

Change your GET method to

[HttpGet]
public ActionResult ProductFields_Create()
{
    var model = new ProductFields
    {
        LisGroups = db.UserGroup.ToList();
    };
    return View(model);
}

Side note: The property could be public IEnumerable<UserGroup> LisGroups { get; set; } public IEnumerable<UserGroup> LisGroups { get; set; } public IEnumerable<UserGroup> LisGroups { get; set; } and then delete the unnecessary use of .ToList()

Then in the view

@foreach (var group in Model.LisGroups)
{
    <div>
        <label>
            @Html.RadioButtonFor(x => x.SelectedGroupID, group.UserGroup_ID, new { id = "" })
            <span>@group.UserGroupNameEn</span>
        </label>
    </div>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM