Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在文本字段中使用多列时保留selectlist中的id_C#_Mysql_Asp.net_Entity Framework_Selectlist - Fatal编程技术网

C# 如何在文本字段中使用多列时保留selectlist中的id

C# 如何在文本字段中使用多列时保留selectlist中的id,c#,mysql,asp.net,entity-framework,selectlist,C#,Mysql,Asp.net,Entity Framework,Selectlist,我试图使用SelectList在下拉列表中显示数据库中的多个列 public ActionResult Create() { var times = db.Show_Courses .Select(x => new { id = x.show_course_id, ti

我试图使用SelectList在下拉列表中显示数据库中的多个列

public ActionResult Create()
    {
        var times = db.Show_Courses
                 .Select(x =>
                         new {
                             id = x.show_course_id,
                             times = x.show_course_before + "," + x.show_course_after
                         });


        ViewBag.course_id = new SelectList(times, "id", "times");
        return View();
    }
它在视图中正确显示,但在数据库中id的值为0

这是我想要在selectlist的textfield中显示两列之前的代码

public ActionResult Create()
    {
        ViewBag.course_id = new SelectList(db.Show_Courses, "show_course_id", "show_course_time_before");
        return View();
    }
这是我在视图中的代码

        <div class="form-group">
        @Html.LabelFor(model => model.course_show_id, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("course show id", (SelectList)ViewBag.course_id, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.course_show_id, "", new { @class = "text-danger" })
        </div>
    </div>

@LabelFor(model=>model.course\u show\u id,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“课程显示id”,(SelectList)ViewBag.course_id,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.course\u show\u id,“,new{@class=“text danger”})

因此,我的问题是如何在选择列表的文本字段中显示2个值而不使id变为0?

您可以手动生成选择列表

比如说,


我个人喜欢使用强类型模型。您可以阅读更多内容

您的代码似乎很好,您需要将下拉列表映射到model:show_course_id-中的相应值,因此请将下拉列表更新为:

@Html.DropDownList("show_course_id", (SelectList)ViewBag.course_id, new { htmlAttributes = new { @class = "form-control" } })

好的,现在我在编译时的视图中有一个错误:System.Collections.Generic.List-Type无法转换为System.Web.Mvc.SelectList。“}
@Html.DropDownList(“课程显示id”,(IList)ViewBag.course\u id,new{htmlAttributes=new{@class=“form control”})
将SelectList替换为IListHanks,但将其插入数据库时,id仍然变为0:(
“课程显示id“
不是有效的Id,因此ModelBinder无法绑定发布的值。您需要阅读。如果从一开始就使用强类型Model,则不会出现此问题。我更改了您提到的内容,但我的idI仍然为0。我尝试了您的解决方案,但没有更改我的源代码的任何其他内容,但Id仍然为0
@Html.DropDownList("show_course_id", (SelectList)ViewBag.course_id, new { htmlAttributes = new { @class = "form-control" } })