Fill a radobuttonlist from a database table in asp.net mvc

enter image description here

I want to create a form as shown above,

You also need to populate Viewer accodrding user groups in the UserGroups table enter image description here

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

Fill in these user groups and get the completed data that I created a new model (so that it can be applied to viewing (1st image))

I followed the abstracts of question 1 and question 2 to do this

this is the 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 the model class for the 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; } } 

page view to view above

 @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 get serious errors with red squiggly lines

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

enter image description here

It is not possible to implicitly convert the type 'System.Web.Mvc.MultiSelectList' to 'System.Collections.Generic.List'

enter image description here

+5
source share
1 answer

You do not need a MultiSelectList (which is a class to use in ListBox() and ListBoxFor() methods). And the cause of the error is because your List<UserGroup> LisGroups , but you are trying to assign typeof MultiSelectList .

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 may be public IEnumerable<UserGroup> LisGroups { get; set; } public IEnumerable<UserGroup> LisGroups { get; set; } public IEnumerable<UserGroup> LisGroups { get; set; } and then remove the unnecessary use of .ToList()

Then in 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> } 
+4
source

Source: https://habr.com/ru/post/1235666/


All Articles