如何从数据库中检索值?

编程入门 行业动态 更新时间:2024-10-28 10:36:35
本文介绍了如何从数据库中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何检索数据库表值并将其填入html表取决于我的MVC4项目中的html文本框值。我是MVC的新手,请帮我这样做。谢谢。 我的代码类似于贝娄 型号是:

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.ComponentModel.DataAnnotations.Schema; 使用 System.ComponentModel.DataAnnotations; 使用 System.Data.Entity; 命名空间 MvcTabletest.Models { [表( tbl_mainProducts)] public class orderfromcatalog { [Key] public int ID { get ; set ; } public string str_CatID { get ; set ; } public string str_SpecialDietID { get ; set ; } public string str_BrandID { get ; set ; } public string str_Description { get ; set ; } public string str_summery { get ; set ; } public string str_ImagePath { get ; set ; } public string Item_ID { get ; set ; } public decimal dec_Price { get ; set ; } } public class MainProductsContext:DbContext { public DbSet< orderfromcatalog> arderfromcatalog { get ; set ; } } }

查看:

< html > < head > < / head > < body > < 表格 id = quickbuy c action = 方法 = 发布 > < table class = qbTable cellpadding = 0 cellspacing = 0 border = 1 > < tr > < th class = qbItemIndex > & nbsp; < / th > < th class = qbItemNumber > 项目编号< / th > < th class = qbItemQty > 数量< / th > < th class = qbItemDesc > 产品详细信息 < / th > < th class = qbItemUtil > & nbsp; < / th > < th class = qbItemPrice > 价格< / th > < th class = qbItemTotal > 总< / th > < / tr > < ; tr id = qbItemRow-1 > < td class = qbItemIndex > 1 < / td > < td class = qbItemNumber > < 输入 id = Item_ID 名称 = onchange = type = text 值 = size = 9 maxlength = 12 / > < / td > < td class = qbItemQty > < / td > < td class = qbItemDesc > < div id = desc-1 > < / div > < / td > < td class = qbItemUtil > < / td > < td class = qbItemPrice > < div id = price-1 > < / div > < / td > < td class = qbItemTotal > < div id = total-1 > < / div > < / td > < / tr > < / form > < / table > < / body > < / html >

解决方案

你需要有一个控制器动作如

public ActionResult GetProductDetails( int id) { // 调用您执行上下文更改的服务方法。 var details = _abcService.GetProductDetails(id); var viewModel = new ProductViewModel { product = details // 这将是类型产品类的列表。 } return 查看(viewModel); }

查看模型如下:

public 列表< product>产品{获取; set ;} < / 产品 >

请评论任何怀疑你有 谢谢 :)

控制器:

public class MainProductsController:Controller { private MainProductsContext db = new MainProductsContext(); public ActionResult Index() { return 查看(db.arderfromcatalog.ToList()); } }

索引视图:

@model IEnumerable< mvctabletest.models.orderfromcatalog> [...] @foreach( var item in Model){ < tr> < td> @ Html.DisplayFor(modelItem = > item.ID) < / td > < td> @ Html.DisplayFor(modelItem = > item.str_CatID) < / td > < td> @ Html.DisplayFor(modelItem = > item.str_BrandID) < / td > < / tr > } [...] < / mvctabletest.models.orderfromcatalog > ;

How Do I retrieve the database table values and fill them in to html table depends up on my html text box value in my MVC4 project. I am new to MVC, please help me to do this. Thank you. I have code like bellow MODEL is:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace MvcTabletest.Models { [Table("tbl_mainProducts")] public class orderfromcatalog { [Key] public int ID { get; set; } public string str_CatID { get; set; } public string str_SpecialDietID { get; set; } public string str_BrandID { get; set; } public string str_Description { get; set; } public string str_summery { get; set; } public string str_ImagePath { get; set; } public string Item_ID { get; set; } public decimal dec_Price { get; set; } } public class MainProductsContext : DbContext { public DbSet<orderfromcatalog> arderfromcatalog { get; set; } } }

VIEW :

<html> <head> </head> <body> <form id="quickbuy" c action="" method="post"> <table class="qbTable" cellpadding="0" cellspacing="0" border="1"> <tr> <th class="qbItemIndex">&nbsp;</th> <th class="qbItemNumber">Item Number</th> <th class="qbItemQty">Qty</th> <th class="qbItemDesc">Product Details</th> <th class="qbItemUtil">&nbsp;</th> <th class="qbItemPrice">Price</th> <th class="qbItemTotal">Total</th> </tr> <tr id="qbItemRow-1"> <td class="qbItemIndex">1</td> <td class="qbItemNumber"> <input id="Item_ID" name="" onchange="" type="text" value="" size="9" maxlength="12"/> </td> <td class="qbItemQty"> </td> <td class="qbItemDesc"> <div id="desc-1"></div> </td> <td class="qbItemUtil"> </td> <td class="qbItemPrice"> <div id="price-1"></div> </td> <td class="qbItemTotal"> <div id="total-1"></div> </td> </tr></form></table> </body> </html>

解决方案

You need to have a controller action Like

public ActionResult GetProductDetails(int id) { //call your service method where you do the context changes. var details = _abcService.GetProductDetails(id); var viewModel = new ProductViewModel { product = details //this would be a list of type product class. } return View(viewModel); }

View Model would go as below

public List<product> products{get; set;} </product>

Please comment with any doubt you have Thanks :)

Controller:

public class MainProductsController : Controller { private MainProductsContext db = new MainProductsContext (); public ActionResult Index() { return View(db.arderfromcatalog.ToList()); } }

Index View:

@model IEnumerable<mvctabletest.models.orderfromcatalog> [...] @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.ID) </td> <td> @Html.DisplayFor(modelItem => item.str_CatID) </td> <td> @Html.DisplayFor(modelItem => item.str_BrandID) </td> </tr> } [...] </mvctabletest.models.orderfromcatalog>

更多推荐

如何从数据库中检索值?

本文发布于:2023-10-28 05:40:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!