2015-11-11 12 views
5

enter image description herePopulate RadioButtonList z tabeli bazy danych w ASP.NET MVC

Chcę utworzyć formularz jak wyżej,

również musiał wypełnić Viewer grup użytkowników accodrding do Grupy stół enter image description here

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

Zapełnij te grupy użytkowników i uzyskaj wypełnione dane Utworzony nowy model (aby mógł on zastosować do widoku (pierwszy obraz))

Śledziłem tezy przepełnienie stosu question 1 i question 2 aby ten

to klasa kontrolera do tego

[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) 
    { 
     ........... 
    } 

jest modelem klasy dla pierwszego obrazu

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 na powyższym widoku

@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> 
} 

ale tutaj jestem coraz większych błędów z czerwonymi liniami falowane

Nazwa „grupa” nie istnieje w bieżącym kontekście

enter image description here

nie można niejawnie przekonwertować typ "System.Web.Mvc.MultiSelectList" do "System.Collections.Generic.List"

enter image description here

+0

@StephenMuecke tak go to, Gdy wybiorę grupę A, powinna ona minąć Grupę As, ID – kez

Odpowiedz

4

Nie potrzebujemy MultiSelectList (która jest klasą dla zastosowania w metodach ListBox() i ListBoxFor()). Przyczyną błędu jest to, że Twoja własność jest List<UserGroup> LisGroups, ale próbujesz przypisać do niej typeof MultiSelectList.

Zmiana metody GET do noty

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

Side: Obiekt może być public IEnumerable<UserGroup> LisGroups { get; set; } a następnie usuń niepotrzebne stosowanie .ToList()

Następnie w widoku

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

Nadal otrzymuję "Grupa nazw" nie istnieje w bieżącym t context' error – kez

+0

Masz na myśli w widoku? –

+0

tak na stronie widoku cshtml, nadal otrzymuję ten błąd z czerwonymi liniami squiqqly – kez

Powiązane problemy