如何从数据库中检索值? [英] How to retrieve values from database?

查看:69
本文介绍了如何从数据库中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索数据库表值并将其填入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 < span class =code-keyword>>
& nbsp; < < span class =code-leadattribute> / 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 >
< < span class =code-leadattribute> / tr >

< ; tr id < span class =code-keyword> = qbItemRow-1 >
< td class = qbItemIndex > 1 < / td >
< td class = qbItemNumber >
< 输入 id = Item_ID 名称 = onchange = type = text = size = 9 maxlength = 12 / >
< / td >
< td class = qbItemQty < span class =code-keyword>>

< / td >
< td class = qbItemDesc >
< div id = desc-1 > < / div >
< span class =code-keyword>< / td
>
< td class = qbItemUtil >
< / td >
< td class = qbItemPrice >
< div id = < span class =code-keyword> price-1 > < < span class =code-leadattribute> / div >
< / td >
< td class = qbItemTotal >
< span class =code-keyword>< 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 < span class =code-comment> // 这将是类型产品类的列表。
}
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>


这篇关于如何从数据库中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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