Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to fetch data from the database, pass the value to modal. and show modal data in view. (without using dataview, databag etc)


i m beginner

What I have tried:

@model MvcDynamicMenuBar.Models.HomePagecs
@{
    ViewBag.Title = "Index";
    //string ITEM_CODE = Model.ITEM_CODE;
    //string ITEM_DESC = Model.ITEM_DESC;
}
@using (Html.BeginForm())
{
<h2>Index</h2>
@Html.LabelFor(model => model.ITEM_CODE)
@Html.LabelFor(model => model.ITEM_DESC)
}




namespace MvcDynamicMenuBar.Models
{
    public class HomePagecs
    {
        public String ITEM_CODE { get; set; }
        public String ITEM_DESC { get; set; }
    }
}



public ActionResult Index()
       {
           try
           {
               objHomePagecs = new HomePagecs();

               String connstr = ConfigurationManager.AppSettings["ConnectionString"];
               OracleConnection STR = new OracleConnection(connstr);
               STR.Open();
               string command = "SELECT ITEM_CODE,ITEM_DESC FROM INV_ITEM_MASTER WHERE ITEM_ID='16048'";
               OracleCommand cmd = new OracleCommand(command, STR);
               OracleDataAdapter sda = new OracleDataAdapter(cmd);
               DataTable dt = new DataTable();
               sda.Fill(dt);
               objHomePagecs.ITEM_CODE = dt.Columns["ITEM_CODE"].ToString();
               objHomePagecs.ITEM_DESC = dt.Columns["ITEM_DESC"].ToString();

           }
           catch(Exception ex)
           {
           }
           return View();
       }



result is
ITEM_CODEITEM_DESC
in webpage value is not coming
Posted
Updated 30-May-17 2:51am
v2
Comments
Suvendu Shekhar Giri 30-May-17 7:17am    
..and what is the issue you are facing?
any error with the tried code?
manish-gusain8909 30-May-17 7:26am    
there is no error, data fetched from database is not showing . ITEM_CODE AND ITEM_DESC IS SHOWING

1 solution

Consult the documentation for LabelFor and you'll see it uses the DisplayName attribute of your model properties which you are not setting so it is using the property name instead. LabelFor is to give descriptive names to your properties. If your model was like this

C#
public class HomePagecs
{
    [DisplayName("Code")]
    public String ITEM_CODE { get; set; }
    [DisplayName("Description")]
    public String ITEM_DESC { get; set; }
}


then you'd get "Code" and "Description" instead. However what you actually want is the value in the property, not the property name so simply use

@Model.ITEM_CODE
@Model.ITEM_DESC


Your other display isssue is simply an HTML issue, HTML does not treat ASCII line breaks etc as line breaks, you need to explicitly say how you want the data separated

@Model.ITEM_CODE<br/>
@Model.ITEM_DESC


That will do an HTML break between the data so it is on different lines.

This is all incredibly basic MVC stuff, I strongly recommend you get a book on MVC or at least go through some tutorials on getting started (google "mvc music store")
 
Share this answer
 
Comments
manish-gusain8909 30-May-17 9:07am    
@model MvcDynamicMenuBar.Models.HomePagecs
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{

Index


<@Model.ITEM_CODE


}

error is coming

Object reference not set to an instance of an object.
F-ES Sitecore 30-May-17 9:18am    
You need to pass the model to the view when you "return View()" so "return View(yourModelHere)". To do so you'll need to move the declaration of your model object before the "try" block begins. As I said, these things are all covered in any basic MVC tutorial, you can't learn something from scratch from forum poosts.

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