Search code examples
asp.net-mvc-3linqentity-framework-4.1html.textboxfor

Compiler error CS1061 when trying to retrieve data for an MVC3 Internet application


I'm creating an MVC3 Internet application to get the data from database.

I'm getting the following error:

Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable<Project.Models.MyModel>' does not contain a definition for 'SId' and no extension method 'SId' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Project.Models.MyModel>' could be found (are you missing a using directive or an assembly reference?)

Here is my code:

Controller:

public ActionResult RetrievedData()
        {
            MyModel mymodel=new MyModel();
            IEnumerable<MyModel> res = mymodel.RetrieveData();
            return View();
        }

Model:

public class MyModel
    {
        public int SId { get; set; }
        public string StrName { get; set; }
        public DateTime? DateCreated { get; set; }

        Test_2Entities entity = new Test_2Entities();
public IEnumerable<MyModel> RetrieveData()
        {
            var query = (from s in entity.StudentInfo
                         where s.SId == 1
                         select new MyModel()
                         {
                             SId = s.SId,
                             StrName = s.SName,
                             DateCreated = s.DateCreated
                         });

            return query.AsEnumerable();

        }

View:

@model IEnumerable<DateAndTimecontrol.Models.MyModel>
    @Html.BeginForm("GetData", "Home", FormMethod.Get)
            {
            Time Picker:<input type="text" id="timepicker2" />
            @Html.TextBoxFor(m=>m.SId,"",new{id="sname"}) //Error at this line

            <br />
            <br />
            Date Picker:<input type="text" id="datepicker2" /><br />
            <input type="text" id="txtName2" /><br />
            <input type="submit" value="GettingDataFromDB"/>
            }

What am I doing wrong?


Solution

  • You're fetching a collection of entities, but trying to treat it as a single entity.

    If you only want to retrieve a single value, you should use First, FirstOrDefault, Single or something similar (depending on your exact needs).

    If you want to really retrieve a collection, you should handle it that way in your view too.