2013-04-11 51 views
0

我想在视图中显示一个复杂的viewmodel,除了一个dropdownlist之外,一切正常。我正在显示一个枚举列表,并按照它应该设置所选对象。但是,由于不明原因,它没有显示选定的价值。我已经尝试了很多次,但失败了,请帮助我。下面的代码给出 -在dropdownlist中的选定值不起作用

枚举:

public enum CommentStatus 
{ 
    NoStatus = 0, 
    Resolved = 1, 
    NotApplicable = 2 
} 

型号:

public class CheckComments 
{ 
    public string EntryId { get; set; } 
    public int Serial { get; set; } 
    public string Comment { get; set; } 
    public DateTime CommentDate { get; set; } 
    public CommentStatus CommentStatus { get; set; } 
} 

public class CheckDataViewModel 
{ 
    public string EntryId { get; set; } 
    public string CheckId { get; set; } 
    public decimal Value { get; set; } 
    public List<CheckComments> CheckCommentsList { get; set; } 

    public List<string> CommentStatusList { get; set; } 
} 

控制器:

public class CheckDataController : Controller 
{ 
    public ActionResult GetEnteredCheckData(string entryId) 
    { 
     var checkDataViewModel = new CheckDataViewModel(); 
     var checkData = new CheckDataManager().GetCheckData(entryId); 

     checkDataViewModel.EntryId = checkData.EntryId; 
     checkDataViewModel.CheckId = checkData.CheckId; 
     checkDataViewModel.Value = checkData.Value; 
     checkDataViewModel.CheckCommentsList = checkData.Comments; 
     checkDataViewModel.CommentStatusList = Enum.GetNames(typeof (CommentStatus)).ToList(); 

      return View("EnteredCheckData", checkDataViewModel); 
    } 
} 

查看:

@model PDCA.Web.Models.CheckDataViewModel 
@{ 
    ViewBag.Title = "EnteredCheckData"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <div class="row"> 
     <div class="span3"> 
      <div class="text-left">@Html.LabelFor(model => model.EntryId)</div> 
     </div> 
     <div class="span3"> 
      <div class="text-left"> 
       @Html.DisplayFor(model => model.EntryId) 
       @Html.HiddenFor(model => model.EntryId) 
      </div> 
     </div> 
     </div> 
    </fieldset> 
    <fieldset> 
     <legend>Check Information</legend> 
     <div class="row"> 
     <div class="span3"> 
      <div class="text-left">@Html.LabelFor(model => model.CheckId)</div> 
     </div> 
     <div class="span3"> 
      <div class="text-left"> 
       @Html.DisplayFor(model => model.Check.CheckId) 
       @Html.HiddenFor(model => model.Check.CheckId) 
      </div> 
     </div> 
     </div> 
    </fieldset> 
    <fieldset> 
     <legend>Comments</legend> 

     <div> 
      <table class="table table-bordered"> 
      <tr> 
       <th> 
        Serial 
       </th> 
       <th> 
        Comment 
       </th> 
       <th> 
        Date 
       </th> 
       <th> 
        Status 
       </th> 
       <th> 
       </th> 
      </tr> 
      @for (int i = 0; i < Model.CheckCommentsList.Count; i++) 
      { 
       <tr> 
       <td> 
        @Html.DisplayFor(modelItem => Model.CheckCommentsList[i].Serial) 
        @Html.HiddenFor(modelItem => Model.CheckCommentsList[i].Serial) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => Model.CheckCommentsList[i].Comment) 
        @Html.HiddenFor(modelItem => Model.CheckCommentsList[i].Comment) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => Model.CheckCommentsList[i].CommentDate) 
         @Html.HiddenFor(modelItem => Model.CheckCommentsList[i].CommentDate) 
        </td> 
        <td> 
         @Html.DropDownListFor(modelItem => Model.CheckCommentsList[i].CommentStatus, new SelectList(Model.CommentStatusList, Model.CheckCommentsList[i].CommentStatus)) 
         @Html.ValidationMessageFor(model => Model.CheckCommentsList[i].CommentStatus) 
        </td> 
        <td> 
         @Html.ActionLink("Update Status", "UpdateCommentStatus", new { Model.EntryId, Model.CheckCommentsList[i].Serial, Model.Check.CheckTitle, Model.Check.CheckId })| 
         @Html.ActionLink("Delete", "DeleteComment", new { Model.CheckCommentsList[i].Serial, Model.EntryId }) 
        </td> 
       </tr> 
      } 
     </table> 
    </div> 

    </fieldset> 
} 
<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

代码太冗长,但我认为这会帮助你理解我的错误。在此先感谢朋友。

+0

我找到了解决方案。选定对象中的简单ToString()已完成它。 :) – Mayeed 2013-04-12 15:00:39

回答

0

解决方案:

@ Html.DropDownListFor(modelItem => Model.CheckCommentsList [I] .CommentStatus,新的SelectList(Model.CommentStatusList,Model.CheckCommentsList [I] .CommentStatus.ToString()))