2012-02-19 8 views
0

これは私のViewModelMVC: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; } 

} 

で、私のビュー内で、私はこのコードAINTの作業に問題がある:

@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を再投入する必要がある場合に注意してください。

これが役立つことを願っています。

関連する問題