从数据库中检索信息并将其丢弃在列表中 [英] retrieve information from the database and throw them down in a list

查看:84
本文介绍了从数据库中检索信息并将其丢弃在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从数据库中检索信息。来自数据库的内容是独特的,远远超出我在这里展示的内容



这是在我的 create.cshtml



It is such that I must retrieve information from the database. The content comes from the database is unique and much more than that as I show here

this is in my create.cshtml

<div class="col-md-6">
   <label>subjects</label>
   <select id="Select1" class="form-control input-lg">
     <option value="1">Danish</option>
     <option value="2">Mathematics</option>
     //There will then be many more after. & I work with Linq
   </select>
</div>





控制器 - > createController.cs有i:





Controllers -> createController.cs have i :

public ActionResult create()
{
   return View();
}





我应该从名为Professional的桌面下载



我在网上搜索过,实际上找不到任何可以用某种方式行动的东西。



问题是如何订购它仍然照顾当我注册成为页面用户时,我只能扔进数据库。



我该怎么做?



Tablen that I should download from called Professional

I have searched around the net and could not really find anything I could to act in some manner.

the problem is how how can I order it still looked after my own so that it is just to throw into the database when you sign up as users of the page.

How do I done this?

推荐答案

在create方法中,需要使用SqlConnection或Entity Framework连接从数据库填充值列表。然后,您可以使用ViewBag变量将数据发送到视图。代码可能如下所示:



In your create method, you need to populated the list of values from the database using SqlConnection or Entity Framework connection. You can then use ViewBag variable to send the data to the view. The code might look as below:

public ActionResult create()
{
var subjects  = dbContext.Subjects.ToList(); // if the Subjects is the table in the database
ViewBag.Subjects = new SelectList(subjects, "SubjectId", "SubjectName","");
return View();
}





这将在创建视图中为您提供ViewBag数据的主题列表。

在视图中,您可以使用Select或DropDownList控件来显示主题。对于Select控件,您可以使用如下:



This will give you the list of subject as ViewBag data in the create view.
In the view you can use either Select or DropDownList control to display the Subjects. For Select control you can use as below:

<select id="subjects" name="subjects" class="selectpicker show-tick form-control" title="">
@foreach (var item in ((IEnumerable<SelectListItem>)ViewBag.Subjects))
{
<option>@item.Text</option>     
}
</select></select>





如果你使用DropDownList然后你需要从控制器视图模型发送数据





If you use DropDownList then you need to send the data from the controller view Model

@Html.DropDownListFor(m=>m.Subject, new SelectList(Model,"SubjectId","SubjectName"));





希望这会有所帮助



Hope this will help


这篇关于从数据库中检索信息并将其丢弃在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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