5

enter image description here

I want to create a form like above,

also have to populate Viewer User Groups accodrding to UserGroups table enter image description here

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

enter image description here

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

enter image description here

1
  • @StephenMuecke yes it is, Once I select Group A , it should pass Group As , ID Commented Nov 11, 2015 at 5:58

1 Answer 1

4

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; } 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>
}
Sign up to request clarification or add additional context in comments.

4 Comments

Im still getting The name 'group' does not exist in the current context error
Do you mean in the view?
yeah in the cshtml view page , Im still getting this error with red squiqqly lines
Ahh got it , I've missed @ sign in front of foreach now this is okay , thanks lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.