简体   繁体   English

DropDownList允许用户选择“名称”,但在数据库中创建“ ID”

[英]DropDownList allow user to select “Name” but create “ID” in the DB

I am trying to create a web application which will allow users to select a training course from DB, then 'apply' to this course. 我正在尝试创建一个Web应用程序,该应用程序将允许用户从DB中选择一个培训课程,然后“应用”到该课程。 There application will be logged in "CourseRequests" table. 该应用程序将被记录在“ CourseRequests”表中。

I have a table called "InternalCourses" 我有一个名为“内部课程”的表格
I have a tabled called "CourseRequests" 我有一个表格叫做“ CourseRequests”

I have build a CREATE.CSHTML page which allows the user to select a course, and then hit 'create' it should create a record in the relevant table. 我已经建立了一个CREATE.CSHTML页面,该页面允许用户选择一个课程,然后点击“创建”,它应该在相关表中创建一条记录。 My problem is because I need the user to select a CourseName, and have the program send back the relevant ID to the database, and discard the coursename. 我的问题是因为我需要用户选择一个CourseName,然后让程序将相关ID发送回数据库,并丢弃该Coursename。 A screenshot of this page looks like so: 该页面的屏幕截图如下所示: 链接

My Controller class: 我的控制器课程:

    // GET: CourseRequests/Create
    public ActionResult Create()
    {
        ViewBag.InternalCourses = db.InternalCourses.ToList();
        ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName");
        ViewBag.ManagerID = new SelectList(db.Employees, "ManagerID", "FirstName");
        ViewBag.CourseID = new SelectList(db.ExternalCourses, "CourseID", "CourseName");
        ViewBag.CourseID = new SelectList(db.InternalCourses, "CourseID", "CourseName");
        ViewBag.CourseID = new SelectList(db.TrainingFeedbacks, "CourseID", "Question");
        return View();
    }

My Create.cshtml 我的Create.cshtml

<!-- CourseID-->
        <div class="form-group">
            @Html.LabelFor(model => model.CourseID, "CourseID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m => m.CourseID, new SelectList(ViewBag.InternalCourses, "CourseID", "CourseName"), new { @class = "form-control" } )
                @Html.ValidationMessageFor(model => model.CourseID, "", new { @class = "text-danger" })
            </div>

My (relevant) model diagram: 我的(相关)模型图: 数据库模型 The image shows a link from InternalCourse.CourseID , to CourseRequest.CourseID please note that the table CourseRequests, does not contain "CourseName" only ID. 该图显示了从InternalCourse.CourseIDCourseRequest.CourseID的链接,请注意,表CourseRequests仅包含“ CourseName” ID。

Please could somebody advise me where I'm going wrong, I can see that I'm trying to put CourseName into ID field but don't get how I can change this. 请有人告诉我我要去哪里哪里,我可以看到我正在尝试将CourseName放入ID字段,但不知道如何更改此设置。

Well, for starters you define ViewBag.CourseID three times, which is a total waste of resources as only the last time will stick. 好吧,对于初学者来说,您需要定义ViewBag.CourseID三次,这完全浪费了资源,因为只有最后一次才能坚持下来。 The first two are just wasted queries: 前两个只是浪费的查询:

ViewBag.CourseID = new SelectList(db.ExternalCourses, "CourseID", "CourseName");
ViewBag.CourseID = new SelectList(db.InternalCourses, "CourseID", "CourseName");
ViewBag.CourseID = new SelectList(db.TrainingFeedbacks, "CourseID", "Question");

Next, when using ViewBag to store your select lists, you need to be careful not to clobber any of your model data. 接下来,在使用ViewBag存储选择列表时,需要注意不要破坏任何模型数据。 Anything in ModelState (which ViewBag is a part of) overrides anything on your model. ModelState任何内容( ViewBag都是其中的一部分)都会覆盖模型中的所有内容。 Therefore, you'll never be able to show the selected value for the property CourseID when you have a select list stored in ViewBag.CourseID because the value will always be the select list itself, not what item was selected. 因此,你就永远无法显示属性选择的值CourseID当你存储在选择列表ViewBag.CourseID因为该值将始终是被选择的项目选择列表本身,而不是。 Use a different name such as ViewBag.CourseChoices . 使用其他名称,例如ViewBag.CourseChoices

Finally, I'm not sure what problem you're actually having here. 最后,我不确定您实际上在这里遇到什么问题。 There is no difficulty in showing one thing as choices in a select list, like a name, while submitting something else, like an id. 在选择列表中将某项显示为选择(例如名称),而提交其他内容(例如ID)则没有困难。 The constructor for SelectList should be called in the following format: SelectList的构造函数应以以下格式调用:

new SelectList(listOfItems, propertyToUseAsValue, propertyToUseAsText)

So, in your scenario, as long as you feed it "CourseID" and "CourseName", in that order, you'll get what you want. 因此,在您的方案中,只要按此顺序输入“ CourseID”和“ CourseName”,就可以得到所需的内容。 It's possible some of these other issues I mentioned are actually the source of your issues. 我提到的其他一些问题实际上可能是您问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM