如何在asp.net mvc中编辑页面使用for循环中的下拉列表

时间:2015-08-13 10:42:52

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我正在从Google编写不同类型的代码,但在下拉列表中显示的值不正确。它在文本框中显示正确的值,但在下拉列表中未显示选定的值。

模型

public Int64 EvaluationInfoID { get; set; }

public Int64 EvalCategoryID { get; set; }

public string EvalCategoryName { get; set; }

public string EvalCategoryDesc { get; set; }

public Int64 UserID { get; set; }

public DateTime? EvaluationDate { get; set; }

public int EvalTypeID { get; set; } 

控制器

var models = new List<EvaluationInfoViewModel>();
ViewBag.GridState = this.GridRouteValues();

string serachCriteria = string.Format("created_by={0} and to_date(to_char(evaldate,'MM/DD/YYYY'),'MM/DD/YYYY')={1} ", Convert.ToInt32(Session["UserID"]), "to_date('" + evaldate.ToShortDateString() + "','MM/DD/YYYY')");

PODService.EvaluationInfo[] getData = service.GetEvaluationInfoBySearchCriteria(serachCriteria);

PODService.EvaluationCategory[] evalcat = service.GetEvalCategoryBySearchCriteria(" EC.Eval_ID= 1");//150811VP: For Self Evaluation Eval_ID=1
PODService.EvaluationType[] evalTypes = service.GetEvaluationTypeList();
ViewData["EVALTYPE"] = new SelectList(evalTypes, "EvaluationType_ID", "EvaluationType_Name");



for (var i = 0; i < getData.Length; i++)
{
    models.Add(new EvaluationInfoViewModel());
}

for (var i = 0; i < models.Count; i++)
{
    string serachCriteria1 = string.Format("created_by={0} and to_date(to_char(evaldate,'MM/DD/YYYY'),'MM/DD/YYYY')={1} ", Convert.ToInt32(Session["UserID"]), "to_date('" + evaldate.ToShortDateString() + "','MM/DD/YYYY')");
    models[i].EvalCategoryID = getData[i].EvalCategory_ID;
    models[i].EvalCategoryName = getData[i].EvalCategoryName;
    //models[i].EvalCategoryDesc = getData[i].Eval_CategoryDescription;
    models[i].UserID = Convert.ToInt64(Session["UserID"]);

    models[i].EvaluationDate = getData[i].EvaluationDate;
    models[i].EvalTypeID = getData[i].EvalTypeID;

    ViewData["EVALTYPEInfo"] = new SelectList(evalTypes, "EvaluationType_ID", "EvaluationType_Name", models[i].EvalTypeID);

查看

@model  List<PODWeb.Models.EvaluationInfoViewModel>
@{
    Layout = null;

}
@using (Html.BeginForm("EditEvaluationInfo", "EvaluationInfo", FormMethod.Post))
{  
    <table border="1px" style="margin-left: 122px">
        <tr>
            <td>
                <table>
                    @for (var i = 0; i < Model.Count(); i++)
                    {
                        @Html.HiddenFor(m=>m[i].UserID)
                        @Html.HiddenFor(m=>m[i].EvaluationDate)
                        @Html.HiddenFor(m =>m[i].EvalTypeID)
                        <tr>
                            <td>
                                @Model[i].EvalCategoryName
                            @Html.EditorFor(m =>m[i].EvalTypeID)
                            </td>
                            <td>

                              @*@Html.DropDownListFor(m => m[i].EvalTypeID, ViewData["EVALTYPEInfo"] as IEnumerable<SelectListItem>, "Select Rating", new { @style = "width:200px" })*@


                            </td>
                        </tr>

                        <tr>
                            <td>
                                @Model[i].EvalCategoryDesc
                                @Html.HiddenFor(m => m[i].EvalCategoryID)
                            </td>
                            <td>

                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <hr />
                            </td>
                        </tr>


                    }
                    <tr>
                        <td>
                            <input type="submit" name="Save" value="Save" class="t-button" style="margin-left: 471px" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

    </table>
}

我想在下拉列表中显示所选的evaltypeid,但它没有显示。

1 个答案:

答案 0 :(得分:0)

您已将下拉列表的代码注释掉:

@*@Html.DropDownListFor(m => m[i].EvalTypeID, 
  ViewData["EVALTYPEInfo"] as IEnumerable<SelectListItem>, 
      "Select Rating", new { @style = "width:200px" })*@

您需要删除外部@**@

@Html.DropDownListFor(m => m[i].EvalTypeID, 
   ViewData["EVALTYPEInfo"] as IEnumerable<SelectListItem>, 
     "Select Rating", new { @style = "width:200px" })

同时从代码中删除@Html.EditorFor(m =>m[i].EvalTypeID)