Click here to Skip to main content
15,888,220 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
controller code
C#
  public class HomeController : Controller
    {
        public static string con_str = @"Data Source=10.0.0.222;Initial Catalog=Student;User ID=IAdmin;Password=sql2008@";
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ViewResult()
        {
            SqlConnection con = new SqlConnection(con_str);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Sturecord", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            //SqlDataReader Myreader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            da.Fill(dt);
            return View("Index", dt);
        }

        
    }
}


partial view code
C#
@model System.Data.DataTable
@using System.Data;
<div id="queryResult">


    <div>

        <table cellpadding="10" border="1">
            <thead>
                <tr>
                    @foreach (DataColumn col in Model.Columns)
                    {
                        <th>@col.ColumnName</th>

                    }
                </tr>
            </thead>
            <tbody>

                @foreach (DataRow row in Model.Rows)
                {
                    <tr>

                        @foreach (DataColumn col in Model.Columns)
                        {
                            <td>@row[col.ColumnName]</td>

                        }
                    </tr>

                }


            </tbody>

Index.cshtml
C#
@model System.Data.DataTable
@using System.Data;

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@using(Html.BeginForm("ViewResult","home",FormMethod.Post))
{
    @Html.TextArea("txtquery");
    @Html.TextBox("btnsubmit", "submit", new { type = "submit" });
}


<div>
    @Html.RenderPartial("~/Views/Shared/_showOp.cshtml",dt)
</div>


What I have tried:

i have shared the above code and I'm unable to call the partial view.

please help me
Posted
Updated 23-Feb-16 1:20am
Comments
F-ES Sitecore 23-Feb-16 5:21am    
What line is the error at?
Member 11127442 23-Feb-16 5:57am    
the name dt doesn't exist in the current context.

Richard MacCutchan 23-Feb-16 6:07am    
Where?
[no name] 23-Feb-16 6:58am    
Where "dt" doesn't exist in current exist - either view page or controller side.

1 solution

C#
DataTable dt = new DataTable("dt");


Change your controller code for datatable initialization like this.
 
Share this answer
 

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