【问题标题】:Retrieving data from Html.DropDownList() in controller (ASP MVC) | string returned?从控制器中的 Html.DropDownList() 中检索数据 (ASP MVC) |返回字符串?
【发布时间】:2009-06-24 18:26:06
【问题描述】:

我有以下问题:

我在站点/banen(当前本地运行的网络服务器)中有一个使用 SQL 数据库的表单。该链接是使用 ADO.net 建立的,并通过以下方式在控制器中实例化:

DBModelEntities _entities;
_entities = new DBModelEntities(); // this part is in the constructor of the controller.

接下来,我使用这个数据库在我的视图中填充一个 Html.DropDownList()。这分两步完成。在控制器端,我们在构造函数中:

ViewData["EducationLevels"] = this.GetAllEducationLevels();

还有一个辅助方法:

public SelectList GetAllEducationLevels()
{
     List<EducationLevels> lstEducationLevels = _entities.EducationLevels.ToList();
     SelectList slist = new SelectList(lstEducationLevels, "ID", "Name");
     return slist;
}

在视图中我有以下内容:

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>

        <!-- various textfields here -->

        <p>
            <label for="EducationLevels">EducationLevels:</label>
            <!-- <%= Html.DropDownList("EducationLevels", ViewData["EducationLevels"] as SelectList)%> -->
            <%= Html.DropDownList("EducationLevels", "..select option..")%>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

现在,当我浏览到创建页面时,表单已正确呈现。 I can select etc. But when selected I have to use that value to save in my new model to upload to the database.这就是它出错的地方。我有以下代码在我的控制器中执行此操作:

//
    // POST: /Banen/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection form)
    {
        // set rest of information which has to be set automatically
        var vacatureToAdd = new Vacatures();
        //vacatureToAdd.EducationLevels = form["EducationLevels"];

        // Deserialize (Include white list!)
        TryUpdateModel(vacatureToAdd);

        // Validate
        if (String.IsNullOrEmpty(vacatureToAdd.Title))
            ModelState.AddModelError("Title", "Title is required!");
        if (String.IsNullOrEmpty(vacatureToAdd.Content))
            ModelState.AddModelError("Content", "Content is required!");

        // Update the variables not set in the form
        vacatureToAdd.CreatedAt = DateTime.Now;                 // Just created.
        vacatureToAdd.UpdatedAt = DateTime.Now;                 // Just created, so also modified now.
        vacatureToAdd.ViewCount = 0;                            // We have just created it, so no views
        vacatureToAdd.ID = GetGuid();                           // Generate uniqueidentifier

        try
        {
            // TODO: Add insert logic here
            _entities.AddToVacatures(vacatureToAdd);
            _entities.SaveChanges();

            // Return to listing page if succesful
            return RedirectToAction("Index");
        }
        catch (Exception e)
        {
            return View();
        }
    } 
    #endregion

它给出了错误:

alt text http://www.bastijn.nl/zooi/error_dropdown.png

我在这方面找到了各种主题,但都说你可以通过使用来检索:

vacatureToAdd.EducationLevels = form["EducationLevels"];

虽然这会为我返回一个字符串。由于我是 ASP.net 的新手,我想我忘记告诉选择要返回的对象而不是字符串。也许这是我制作 SelectList 的部分中的 selectedValue,但我不知道如何正确设置它。当然,我也可以在支线完成。

旁注:目前我正在考虑创建一个单独的模型,例如 here

感谢任何帮助。

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    您不能从通常由 Html.DropDownList() 方法呈现的

    【讨论】:

    • 谢谢,我以前做过,但我认为我返回了一些奇怪的字符串。但是在你这么说之后,我记得我当然是把唯一标识符作为字符串来获取的。所以这就是它出错的原因。期待一个数字或名称,但得到了唯一标识符。
    猜你喜欢
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多