Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Controller code

public class AboutUsController : Controller
    {
       
        public ActionResult Index()
        {

            CMSAboutus dbhandle = new CMSAboutus();
            ModelState.Clear();
            return View(dbhandle.GetAboutTitlepage().ToList());
}


My Class 


public List<CMSAboutus> GetAboutTitlepage()
       {
           connection();
           List<CMSAboutus> studentlist = new List<CMSAboutus>();

           SqlCommand cmd = new SqlCommand("GetAboutpagetitle", con);
           cmd.CommandType = CommandType.StoredProcedure;
         //  cmd.Parameters.AddWithValue("@ID", smodel.id);
           SqlDataAdapter sd = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();

           con.Open();
           sd.Fill(dt);
           con.Close();

           foreach (DataRow dr in dt.Rows)
           {
               studentlist.Add(
                   new CMSAboutus
                   {
                       // Id = Convert.ToInt32(dr["Id"]),
                       Section_Title = Convert.ToString(dr["Section_Title"]),
                       Title_desc = Convert.ToString(dr["Title_desc"]),

                   });
           }
           return studentlist;
       }




Index View


C#
<pre>@using GlobalxVisa_Immigration.Areas.Admin.Models
@model CMSAboutus

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

<div class="container">
    <br />

    <div class="row">
        <div class="modal-content">
            <div class="card-header  bg-primary"><span style="color:white;">About Page Title</span></div>

            <div class="card-body">
               
                    
                        <section>
                            @using (Html.BeginForm("Index", "AboutUs", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                                @Html.AntiForgeryToken()

                                <h5>
                                    class="fa fa-link">
                                   
                                </h5>

                                <hr />
                                if (Model.GetAboutTitlepage() != null && Model.GetAboutTitlepage().Count > 0)
                                {




What I have tried:

datalist data pass to index cshtml page
Posted
Comments
[no name] 11-Sep-20 14:44pm    
You've defined your model as a "class" yet are passing a collection. Also, a ToList() of a List<> accomplishes nothing.
Nishant.Chauhan80 11-Sep-20 14:54pm    
how to define my code please tell me

return View(dbhandle.GetAboutTitlepage().ToList<>); //error
[no name] 11-Sep-20 15:06pm    
Create a class to wrap around the collection, and reference that.
Nishant.Chauhan80 11-Sep-20 15:22pm    
How sir please give me some example
F-ES Sitecore 12-Sep-20 10:11am    
Your view expects a single item but you are passing it a list of items. We can't tell you how to fix this, you need to decide if your view needs to change to accommodate a list of items, or if your controller has to change to pass only a single item.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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