Cannot implicitly convert type 'System.Collections.Generic.List<ERP_APP.Models.BigbaleView>' to 'System.Collections.Generic.List<ERP_APP.Models.ProbaleMV>'

Analyst_SQL 3,551 Reputation points
2023-03-10T12:47:06.8766667+00:00

I am facing following error

	Cannot implicitly convert type 'System.Collections.Generic.List<ERP_APP.Models.BigbaleView>' to 'System.Collections.Generic.List<ERP_APP.Models.ProbaleMV>'

i am using below two model

   public class ProbaleMV
    {

        public List<ProbaleMV> ShowallBigBale { get; set; }


        public int Prdno { get; set; }

        public Nullable<int> OrderNo { get; set; }

        public int CodeItem { get; set; }
        public string Descriptionitem { get; set; }

        public string Secnam { get; set; }
        public string CName { get; set; }
        public Nullable<int> prdqty { get; set; }
     

        [DataType(DataType.Date, ErrorMessage = "Date only")]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]


        public Nullable<System.DateTime> EntryDate { get; set; }






        public List<ProbaleMV> Prdnoa { get; set; }
    }

   public class BBDetailView
    {
        SqlConnection con = new SqlConnection("data source=SERVER1\\SQLEXPRESS;initial catalog=SilverProduction;integrated security=True;MultipleActiveResultSets=True;");

        public List<BigbaleView> Selectalldata()
        {
           
          
            List<BigbaleView> custlist = null;
            try
            {

                SqlCommand cmd = new SqlCommand("SP_ProducewiseBp", con);
                cmd.CommandType = CommandType.StoredProcedure;
        

                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                custlist = new List<BigbaleView>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    BigbaleView cobj = new BigbaleView();
                    cobj.Prdno = Convert.ToInt32(ds.Tables[0].Rows[i]["Prdno"].ToString());
                    cobj.Descriptionitem = ds.Tables[0].Rows[i]["Descriptionitem"].ToString();
                    cobj.Secnam = ds.Tables[0].Rows[i]["Secnam"].ToString();

                    custlist.Add(cobj);
                }
                return custlist;
            }
            catch
            {
                return custlist;
            }
            finally
            {
                con.Close();
            }
        }
    }

Controller

 public ActionResult BBDetail()
        {


     


            BigbaleView objCustomer = new BigbaleView();
            BBDetailView bbd = new BBDetailView(); //calling class DBdata    
            objCustomer.ShowallBigBale = bbd.Selectalldata();
            return View(objCustomer);






        }



View

@model IEnumerable<ERP_APP.Models.BigbaleView>

@{

}


<div class="card">
    <div class="card-header">
        <div class="text-sm">
            <div class="card-title">Item List</div>
        </div>
    </div>
    <div class="card-body">
        @Html.ActionLink("Create New", "CreaterEmployee", null, new { @class = "btn btn-primary" })

        <hr />
        @using (Html.BeginForm("BBDetail", "BigBale", FormMethod.Post))
        {
            <div class="form-horizontal">
                <div class="form-group form-group-sm">
                    <div class="col-md-8">

                        @*<input name="start" type="date" id="datepicker" class="form-control" />*@
                        <input name="start" type="date" id="start" class="form-control" value="@Request["start"]" />
                    </div>
                </div>
                <div class="form-group form-group-sm">
                    <div class="col-md-8">
                        <input name="end" type="date" id="end" class="form-control" value="@Request["end"]" />
                        @*<input name="end" type="date" id="datepicker" class="form-control" />*@
                    </div>
                </div>
                <div class="form-group-sm">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Get" onclick="SubmitForm()" class="btn btn-default" /> <span> </span>
                    </div>
                </div>
            </div>
        }
        <table class="table table-striped my-4 w-100" id="datatable2">
            <thead>
                <tr>

                    <th>
                        @Html.DisplayNameFor(model => model.Descriptionitem)
                    </th>

                    <th>




                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.ShowallBigBale)
                  
                    {
                        <tr>

                            <td>
                                @Html.DisplayFor(modelItem => item.Descriptionitem)
                            </td>


                            <td>
                                @Html.ActionLink("Edit", "EditItem", new { codiitem = item.CodeItem }, new { @class = "btn btn-warning" }) |




                                @Html.ActionLink("Delete", "Delete", new { }, new { @class = "btn btn-danger" })
                            </td>
                        </tr>
                    }

            </tbody>
        </table>
    </div>
</div>
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,263 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,268 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2023-03-11T13:28:11.99+00:00

    it will be better to provide me complete example ,will take time to approach on you level

    You have to understand code is the tools forum member use to communicate. If the code is unclear, incomplete, and does not follow standard coding patterns then we cannot communicate efficiently. This is why I recommend learning MVC fundamentals and MVC standard coding patterns. We can speak the same coding language if you follow standards.

    I tried my best to create an example based on your code. Unfortunately, your code is incomplete, unclear, does not follow standard coding patterns. The flow does not make a lot of sense or there are bits missing which would clarify the intent. Therefore the code example below which is based on your code technically works but is nonsensical.

    URL

    https://localhost:44321/Bigbale/index/?start=2023-01-01&end=2023-04-01
    
    

    Model

        public class BigbaleView
        {
            [Key]
            public int Prdno { get; set; }
            public int CodeItem { get; set; }
            public string Descriptionitem { get; set; }
            public string Secnam { get; set; }
        }
    

    Your data access

    public class BBDetailView
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString);
            public List<BigbaleView> Selectalldata()
            {
                List<BigbaleView> custlist = null;
                try
                {
                    SqlCommand cmd = new SqlCommand("SP_ProducewiseBp", con);
                    cmd.CommandType = CommandType.StoredProcedure;
    
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    custlist = new List<BigbaleView>();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        BigbaleView cobj = new BigbaleView();
                        cobj.Prdno = Convert.ToInt32(ds.Tables[0].Rows[i]["Prdno"].ToString());
                        cobj.Descriptionitem = ds.Tables[0].Rows[i]["Descriptionitem"].ToString();
                        cobj.Secnam = ds.Tables[0].Rows[i]["Secnam"].ToString();
    
                        custlist.Add(cobj);
                    }
                    return custlist;
                }
                catch
                {
                    return custlist;
                }
                finally
                {
                    con.Close();
                }
            }
        }
    

    Controller

        public class BigbaleController : Controller
        {
            // GET: Bigbale/?start=2023-01-01&end=2023-04-01
            public ActionResult Index(string start, string end)
            {
                BBDetailView DataAccess = new BBDetailView();
                List<BigbaleView> data = DataAccess.Selectalldata();
                return View(data);
            }
    
            [HttpPost]
            public ActionResult BBDetail(string start, string end)
            {
                ViewBag.Start = start;
                ViewBag.End = end;
                return View();
            }
        }
    

    Index View

    @model List<MvcIdentity.Models.BigbaleView>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <div class="card">
        <div class="card-header">
            <div class="text-sm">
                <div class="card-title">Item List</div>
            </div>
        </div>
        <div class="card-body">
            @Html.ActionLink("Create New", "CreaterEmployee", null, new { @class = "btn btn-primary" })
    
            <hr />
            @using (Html.BeginForm("BBDetail", "BigBale", FormMethod.Post))
            {
                <div class="form-horizontal">
                    <div class="form-group form-group-sm">
                        <div class="col-md-8">
                            <input name="start" type="date" id="start" class="form-control" value="@Request["start"]" />
                        </div>
                    </div>
                    <div class="form-group form-group-sm">
                        <div class="col-md-8">
                            <input name="end" type="date" id="end" class="form-control" value="@Request["end"]" />
                        </div>
                    </div>
                    <div class="form-group-sm">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Get" onclick="SubmitForm()" class="btn btn-default" /> <span> </span>
                        </div>
                    </div>
                </div>
            }
            <table class="table table-striped my-4 w-100" id="datatable2">
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model[0].Descriptionitem)
                        </th>
                        <th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Descriptionitem)
                            </td>
                            <td>
                                @Html.ActionLink("Edit", "EditItem", new { codiitem = item.CodeItem }, new { @class = "btn btn-warning" }) |
                                @Html.ActionLink("Delete", "Delete", new { }, new { @class = "btn btn-danger" })
                            </td>
                        </tr>
                    }
    
                </tbody>
            </table>
        </div>
    </div>
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        <script>
            function SubmitForm() {
                return;
            }
        </script>
    }
    
    

    BBDetail View

    
    @{
        ViewBag.Title = "BBDetail";
    }
    
    <h2>BBDetail</h2>
    
    <div>@ViewBag.Start</div>
    <div>@ViewBag.End</div>
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2023-03-10T15:00:12.7533333+00:00

    The Selectalldata() method returns a List<BigbaleView> type.

    public List<BigbaleView> Selectalldata()
    

    The results of Selectalldata() are assigned to objCustomer.ShowallBigBale.

    objCustomer.ShowallBigBale = bbd.Selectalldata();
    

    ShowallBigBale is a List<ProbaleMV> type not List<BigbaleView>.

    public List<ProbaleMV> ShowallBigBale { get; set; }
    

    C# is a strongly typed language which does not allow assigning a two different types to one another like List<BigbaleView> and List<ProbaleMV>.

    I'm not exactly sure what trying to do but it looks like you are combining a list view with a create/update view. I recommend simplifying the ViewModel

        public class MyModel
        {
            public int Prdno { get; set; }
            public int? OrderNo { get; set; }
            public int CodeItem { get; set; }
            public string Descriptionitem { get; set; }
            public string Secnam { get; set; }
            public string CName { get; set; }
            public int? prdqty { get; set; }
            [DataType(DataType.Date, ErrorMessage = "Date only")]
            [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
            public DateTime? EntryDate { get; set; }
        }
    

    If you need a List<MyModel> then pass it in the ViewBag. That should make create and update easier.

    As recommended several times in your other thread, set aside time to learn MVC fundamental programming patters. The tutorials on this site are very good.