Asp.net mvc 3 将ViewBag中的at插入相应的实体对象中,以便它可以作为下拉帮助器的源列表-是吗?是的,还是使用强类型模型 public ActionResult Index() { var context = new HBModel.

Asp.net mvc 3 将ViewBag中的at插入相应的实体对象中,以便它可以作为下拉帮助器的源列表-是吗?是的,还是使用强类型模型 public ActionResult Index() { var context = new HBModel.,asp.net-mvc-3,selectlist,Asp.net Mvc 3,Selectlist,将ViewBag中的at插入相应的实体对象中,以便它可以作为下拉帮助器的源列表-是吗?是的,还是使用强类型模型 public ActionResult Index() { var context = new HBModel.HBEntities(); var query = from q in context.tblQuestions.Include("tblCategory") select q; var questions =


将ViewBag中的at插入相应的实体对象中,以便它可以作为下拉帮助器的源列表-是吗?是的,还是使用强类型模型
    public ActionResult Index()
    {
        var context = new HBModel.HBEntities();
        var query = from q in context.tblQuestions.Include("tblCategory") select q;
        var questions = query.ToList();

        ViewBag.Categories = new SelectList(context.tblCategories, "CategoryID", "CategoryName"); 

        return View(questions);
    }
@model IEnumerable<HBModel.tblQuestion>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Question
        </th>
        <th>
            Answer
        </th>
        <th>
            AnswerSource
        </th>
        <th>
            Category
        </th>
        <th>
            Action
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @item.Question
        </td>
        <td>
            @item.Answer
        </td>
        <td>
            @item.AnswerSource
        </td>
        <td>
            @item.tblCategory.CategoryName 

            @*This one works, but cannot initialize the selected item to be current database value*@
            @Html.DropDownList("Categories")

            @*compile error - CS0200: Property or indexer 'System.Web.Mvc.SelectList.SelectedValue' cannot be assigned to -- it is read only*@
            @*@Html.DropDownListFor(m => item.QuestionCategory, (ViewBag.Categories as SelectList).SelectedValue = item.QuestionCategory)*@

            @*error {"DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CategoryId'."}*@
            @Html.DropDownListFor(m => item.QuestionCategory, new SelectList(ViewBag.Categories, "CategoryId", "CategoryName"))

            @*error - {"DataBinding: 'System.Char' does not contain a property with the name 'CategoryId'."}*@
            @Html.DropDownListFor(m => item.QuestionCategory, new SelectList("Categories", "CategoryId", "CategoryName"))
)

        </td>
        <td style="width: 100px">
            @Html.ActionLink("Edit", "Edit", new { id = item.QuestionID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.QuestionID })
        </td>
    </tr>
}

</table>
@Html.DropDownListFor(model => model.IdAccountFrom, ((IEnumerable<FlatAdmin.Domain.Entities.Account>)ViewBag.AllAccounts).Select(option => new SelectListItem {
        Text = (option == null ? "None" : option.AccountName), 
        Value = option.AccountId.ToString(),
        Selected = (Model != null) && (option.AccountId == Model.IdAccountFrom)
    }), "Choose...")
    @Html.ValidationMessageFor(model => model.IdAccountFrom)