2012-02-19 2 views
0

이 내 뷰 모델MVC : 내 데이터베이스에서 데이터를 가져 와서 viewmodel을 사용하여 드롭 다운 목록에 데이터를 표시하는 방법은 무엇입니까?

public class IndexViewModel 
{ 
    public string QuestionText { get; set; } 
    public string Sname { get; set; } 
    public string Cname { get; set; } 
    public int CID { get; set; } 
    public int SID { get; set; } 
    public Question question { get; set; } 
    public CoreValue corevalue { get; set; } 
    public SubjectType subjecttype { get; set; } 

} 

하고 나의보기 안에 나는 문제를이 코드 아닌가요의 작업이 있습니다

내가 오류 메시지가 없습니다
@model NKI3.ViewModels.IndexViewModel 

@using (Html.BeginForm()) { 
<fieldset> 
<div class="editor-label"> 
@Html.DropDownListFor(model => model.Sname) 
</div> 
</fieldset> 
} 

"방법에 대한 과부하가 'DropDownListFor 1 개 인자를을"

해결책은 무엇입니까?

감사합니다.

답변

0
public class AdminController : Controller 
{ 
    AdminRepository AdminRep = new AdminRepository(); 

    public ActionResult Index() 
    { 
     List<Question> ListQuestions = AdminRep.GetAllQuestions(); 
     var model = new AdminIndexViewModel(); 
     model.QuestionList = new List<QuestionViewModel>(); 
     foreach (var item in ListQuestions) 
     { 
      var QuestionViewModel = new QuestionViewModel(); 
      model.QuestionList.Add(QuestionViewModel); 
      QuestionViewModel.QuestionText = item.QuestionText; 
      QuestionViewModel.QuestionId = item.Id; 
      QuestionViewModel.CoreValues = new List<string>(); 
      foreach (var CoreValues in item.CoreValue) 
      { 
       QuestionViewModel.CoreValues.Add(CoreValues.Name); 
      } 
      QuestionViewModel.SubjectTypes = new List<string>(); 
      foreach (var SubjectType in item.SubjectType) 
      { 
       QuestionViewModel.SubjectTypes.Add(SubjectType.Name); 
      } 
     } 
     return View(model); 
    } 

보기 :

@foreach (var item in Model.QuestionList) { 
<tr> 
    <td> 
    @Html.DisplayFor(modelItem => item.QuestionText) 
    </td> 
    <td> 
    @string.Join(", ", item.SubjectTypes) 
    </td> 
    <td> 
    @string.Join(", ", item.CoreValues) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", new { id = item.QuestionId}) | 
     @Html.ActionLink("Delete", new { id = item.QuestionId }) 
    </td> 
</tr> 
0

당신은 내가 채우 할 목록과 컨트롤러에 ViewBag.ListofSName을 채우는 것이 예에서는

@Html.DropDownListFor(model => model.Sname, ((IEnumerable<SNameList>)ViewBag.ListofSName).Select(option => new SelectListItem 
              { 
               Text = (option == null ? "None" : option.Description), 
               Value = option.Id.ToString(), 
               Selected = (Model != null) && (option.Id == Model.SName) 
              }), "Choose...", new { @class = "full-width" }) 

아래의 예를 참조 드롭 다운 목록을 채울 목록 전달되지 않습니다 의 드롭 다운리스트. 이 문제는 포스트 백에 유효성 검사가 있고 ViewBag를 다시 채워야하는 경우에주의해야합니다.

이 도움이 되었기를 바랍니다.

관련 문제