Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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 :
XML
<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>
Posted
Updated 26-Sep-14 1:17am
v3
Comments
Kornfeld Eliyahu Peter 17-Sep-14 3:54am    
And the question is?
RAJU Kandarapu 27-Sep-14 3:23am    
When i enter the Item Number, i want to retrive Product Details and price from sql database

You need to have a controller action Like
C#
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
C#
public List<product> products{get; set;}

</product>


Please comment with any doubt you have
Thanks
:)
 
Share this answer
 
Controller:
C#
public class MainProductsController : Controller
{
    private MainProductsContext db = new MainProductsContext ();

    public ActionResult Index()
    {
        return View(db.arderfromcatalog.ToList());
    }
}


Index View:
C#
@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>
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 26-Sep-14 10:52am    
..and how do you make sure the question is answered? Please try to answer the question using some description too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900