在控制器检索从Html.DropDownList()数据(ASP MVC)|返回的字符串? [英] Retrieving data from Html.DropDownList() in controller (ASP MVC) | string returned?

查看:157
本文介绍了在控制器检索从Html.DropDownList()数据(ASP MVC)|返回的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

我有正在使用SQL数据库网站/ banen(目前本地运行的网络服务器)的形式。链路使用ADO.net制成,被实例化在控制器通过以下方式:

I have a form in site/banen (currently local running webserver) which is using a SQL database. The link is made using ADO.net and is instantiated in the controller in the following way:

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

接下来,我使用这个数据库,以填补Html.DropDownList()在我看来。这是在两个步骤中完成。在控制器方面,我们在构造函数中:

Next, I use this database to fill a Html.DropDownList() in my view. This is done in two steps. At the controller side we have in the constructor:

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

和一个辅助方法:

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

在视图中我有以下几点:

In the view I have the following:

<% 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>

<% } %>

现在,形式正确,当我浏览到创建页面呈现。我可以选择等,但选择时,我不得不使用该值在我的新模式,以节省上传到数据库中。这是它出错。我有以下的code在我的控制器来做到这一点:

Now, the form is rendered correctly when I browse to the create page. I can select etc. But when selected I have to use that value to save in my new model to upload to the database. This is where it goes wrong. I have the following code to do this in my controller:

//
    // 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

它给人的错误:

我已经找到不同主题的这一点,但都说你可以只用检索:

I have found various topics on this but all say you can retrieve by just using:

vacatureToAdd.EducationLevels = form["EducationLevels"];

虽然这返回一个字符串我。由于我是新来的ASP.net我想我忘了告诉,选择返回,而不是一个字符串的对象。也许这是在我做我的SelectList,但我无法弄清楚如何正确设置这个部分了selectedValue。当然,我也可以是完整的一个侧钻。

Though this returns a string for me. Since I'm new to ASP.net I think I am forgetting to tell to select the object to return and not a string. Maybe this is the selectedValue in the part where I make my SelectList but I can't figure out how to set this correctly. Of course I can also be complete on a sidetrack.

旁注:我目前想有一个单独的模型一样的这里

Sidenote: currently I'm thinking about having a seperate model like here.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

您不能从通常的&LT返回一个对象;选择&GT;标签至极由Html.DropDownList渲染()方法,但只有字符串变量可以返回。在EducationLevels对象的情况下,您的ID将被发送到服务器。你应该定义和使用一个多个自定义的辅助方法,通过ID来重建该对象。

You can't return an object from usual <SELECT> tag wich is rendered by Html.DropDownList() method, but only string variable could be returned. In your case ID of EducationLevels object will be send to the server. You should define and use one more custom helper method to reconstruct this object by ID.

这篇关于在控制器检索从Html.DropDownList()数据(ASP MVC)|返回的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆