MVC DropDownList中从一个数据库表填充和发送到另一个数据库表 [英] MVC DropDownList populate from one DB table and send to another DB table

查看:124
本文介绍了MVC DropDownList中从一个数据库表填充和发送到另一个数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有从资格表中提取数据,然后在HttpPost它存储在DropDownList选择在员工表中的资格列中的值一个DropDownList。我是很新的MVC和我真的不知道语法很好。我也不知道如何使用jQuery,VB等,很抱歉,如果有一个帖子在那里,包括这和我从来没见过,因为我不知道我在看你。

I would like to have a dropdownlist which pulls data from a qualification table and then on HttpPost it stores the value selected in the dropdownlist to a "qualification" column on the employee table. I'm very new to MVC and I don't really know the syntax very well. I also don't know how to use jquery, vb etc. so sorry if there is a post out there that covers this and I've not seen it because I don't know what I'm looking at.

这是目前在做的是对我最好的尝试:

This is currently my best attempt at doing this:

控制器:

private Database1Entities db = new Database1Entities();
...
...
...
    // GET: /Home/CreateEmp
            public ActionResult CreateEmp()
            {
                ViewBag.QualList = new SelectList(db.Qualifications, "Id", "qual");
                return View();
            }
            // POST: /Home/CreateEmployee
            [HttpPost, ActionName("CreateEmployee")]
            public ActionResult CreateEmpResult(Employee emp)
            {
                if (ModelState.IsValid)
                {
                    db.Employees.Add(emp);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.QualList = new SelectList(db.Qualifications, "Id", "qual", emp.qualification);
                return View(emp);
            }

创建员工查看:

@model MvcDropDownList.Models.Employee
    @using (Html.BeginForm("CreateEmployee", "Home", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <table>
        <!-- other fields removed for sake of simplicity -->
        <tr>
            <td>
                @Html.Label("Select Your Qualification:")
            </td>

            <td>
                @Html.DropDownList("QualList", "--Select--")
            </td>
        </tr>
    </table>
    }



表结构:

资格表:ID,QUAL

Qualifications Table: Id, qual

Employee表:ID,名字,姓氏,性别,学历

Employee Table: Id, firstName, surName, gender, qualification

我的代码从资格表成功获取数据,但在员工不张贴的资格列表。

My code successfully gets the data from the "Qualification" table but doesn't post it to the "qualification" column in the "employee" table.

任何建议,将不胜感激,谢谢!

Any suggestions will be greatly appreciated, Thank you!

推荐答案

您应该绑定你的DropDownList给你点击Save按钮时返回模式

You should bind your dropdownlist to the model you're returning when clicking the Save button.

在换句话说,这样的:

@Html.DropDownList("QualList", "--Select--")

应该变成这样的:

@Html.DropDownListFor(model => model.Qualification_Id, (SelectList)ViewBag.QualList)

修改斯蒂芬Muecke指出的那样,你需要将Viewbag值转换为的SelectList

Edit As Stephen Muecke pointed out, you need to cast the Viewbag value to a SelectList.

注意有用于 DropDownListFor多种可能性()。我只挑最简单的一个清晰的缘故,但你可以添加可选标签等。

Note There are multiple possibilities for DropDownListFor(). I only picked the easiest one for clarity's sake, but you could add your optional label etc.

这将其绑定到你的模型( EMP ):

This binds it to your model (emp):

public ActionResult CreateEmpResult(Employee emp)

注意:如果你的员工类已经正确安装并具有需要外键(链接到资格表),应该已经有一个 .Qualification_Id (或类似名称)在员工类(假设类是实际的数据库实体)

Note: If your Employee class was correctly set up and has the needed Foreign keys (link to the Qualification table), there should already be a .Qualification_Id (or similar name) in your Employee class (assuming that class is your actual database entity).

这些变化将包括你需要的一切:

These changes will cover everything you need:


  • 将DropDownList现在选择的值写入 emp.Qualification_Id

  • 在您的控制器的方法,您 EMP 保存到数据库中。

  • EF将使用FK字段( emp.Qualification_Id )以确保正确的值将将在数据库中创建新的雇员行中写的。

  • The dropdownlist now writes the selected value into emp.Qualification_Id.
  • In your controller method, you save emp to the db.
  • EF will use the FK fields (emp.Qualification_Id) to make sure the correct values will be written in the new Employee row that will be created in the database.

作为一般规则,总是试图控制(dropdownlists,文本框,...)绑定到你的模型的属性。如果你这样做,你并不需要编写特定的代码做基本上是相同的。

As a general rule, always try to bind controls (dropdownlists, textboxes, ...) to a property of your model. if you do that, you don't need to write specific code to do basically the same.

这篇关于MVC DropDownList中从一个数据库表填充和发送到另一个数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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