locked
another Error message RRS feed

  • Question

  • User-2012457684 posted

    I am getting the following error message

    Compiler Error Message: CS1061: 'IEnumerable<GetCustomJobPattern_Result>' does not contain a definition for 'JobName' and no extension method 'JobName' accepting a first argument of type 'IEnumerable<GetCustomJobPattern_Result>' could be found (are you missing a using directive or an assembly reference?)

    here are the models

    public partial class GetCustomJobPattern_Result
    {
        public string JobName { get; set; }
        public string A1 { get; set; }
        public string A2 { get; set; }
        public string A3 { get; set; }
        public string A4 { get; set; }
        public string A5 { get; set; }
        public string A6 { get; set; }
        public string A7 { get; set; }
        public string D1 { get; set; }
        public string D2 { get; set; }
        public string D3 { get; set; }
        public string D4 { get; set; }
        public string D5 { get; set; }
        public string D6 { get; set; }
        public string D7 { get; set; }
        public string D8 { get; set; }
        public string D9 { get; set; }
        public string D10 { get; set; }
    }
    
    public class CustomPattern
    {
        JobDatabaseEntities db = new JobDatabaseEntities();
        public string SecAccount { get; set; }
        public long JobID { get; set; }
        public List<GetCustomJobPattern_Result> JobPattern()
        { 
            return db.GetCustomJobPattern(JobID, SecAccount).ToList();
        }
    }

    And my controller

    // GET: Patterns/Edit
    public ActionResult Edit(long? id)
    {
        ViewData["SecAccount"] = System.Web.HttpContext.Current.Session["SecAccount"] as String;
        if (ViewData["SecAccount"] != null)
        { 
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ViewBag.JobID = Convert.ToInt64(id);
            CustomPattern cp = new CustomPattern();
            cp.JobID = Convert.ToInt64(id);
            cp.SecAccount = ViewData["SecAccount"].ToString();
            List<GetCustomJobPattern_Result> pattern = cp.JobPattern();
            foreach(var item in pattern)
            {
                ViewBag.JobName = item.JobName;
            }
                    
            return View(pattern);  
                    
                        
        }
        ViewData["ReturnUrl"] = "Patterns/Index";
        return View("LoggedOut");
    }

    and the view

    @model IEnumerable<Admin.Models.GetCustomJobPattern_Result>
    @{
        ViewBag.Title = "Edit Custom Job Pattern";
    }
    
    <h2>Edit Custom Job Pattern</h2>
    
    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form" }))
    {
        @Html.AntiForgeryToken()
        <input type="hidden" name="SecAccount" value="@ViewData["SecAccount"].ToString()" />
        <input type="hidden" name="JobID" value="@ViewBag.JobID.ToString()" />
        <div class="form-group">
            <div class="row">
                @Html.LabelFor(model => model.JobName , htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.JobName, new { htmlAttributes = new { @class = "form-control", required="required", maxlength="200" } })
                    @Html.ValidationMessageFor(model => model.JobName, "", new { @class = "error" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                @Html.LabelFor(model => model.A1, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A1, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A1, "", new { @class = "error", required="required" })
                </div>
           </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                    @Html.LabelFor(model => model.A2, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A2, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A2, "", new { @class = "error", required="required" })
                </div>
           </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.A3, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A3, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A3, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.A4, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A4, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A4, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                @Html.LabelFor(model => model.A5, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A5, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A5, "", new { @class = "error", required="required" })
                </div>
           </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                    @Html.LabelFor(model => model.A6, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A6, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A6, "", new { @class = "error", required="required" })
                </div>
           </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.A7, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.A7, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.A7, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                @Html.LabelFor(model => model.D1, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D1, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D1, "", new { @class = "error", required="required" })
                </div>
           </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                @Html.LabelFor(model => model.D2, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D2, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D2, "", new { @class = "error", required="required" })
                </div>
            </div>      
        </div>
    
        <div class="form-group">
            <div class="row">
                @Html.LabelFor(model => model.D3, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D3, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D3, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.D4, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D4, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D4, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.D5, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D5, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D5, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                @Html.LabelFor(model => model.D6, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D6, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D6, "", new { @class = "error", required="required" })
                </div>
           </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.D7, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D7, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D7, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.D8, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D8, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D8, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
    
        <div class="form-group">
           <div class="row">
                @Html.LabelFor(model => model.D9, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
            <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                @Html.EditorFor(model => model.D9, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                @Html.ValidationMessageFor(model => model.D9, "", new { @class = "error", required="required" })
            </div>
           </div>
        </div>
    
        <div class="form-group">
            <div class="row">
                    @Html.LabelFor(model => model.D10, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-6 com-md-4 col-lg-3" })
                <div class="col-xs-12 col-sm-6 com-md-8 col-lg-9">
                    @Html.EditorFor(model => model.D10, new { htmlAttributes = new { @class = "form-control pattern",  maxlength="2", required="required" } })
                    @Html.ValidationMessageFor(model => model.D10, "", new { @class = "error", required="required" })
                </div>
            </div>
        </div>
     
        
    
        <div class="form-group">
            <div class="row">
                <div class="col-xs-12 col-md-offset-4 col-md-8 col-lg-offset-3 col-lg-9">
                    <input type="submit" value="Add Pattern" class="btn btn-primary" />
                </div>
            </div>
        </div>
        
    
    }
    
    @section scripts{
        <script type="text/javascript" src="~/Scripts/scripts.js"></script>
        <script type="text/javascript" src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>       
        
    }

    When I do a foreach loop inside the controller I can get every value.   When I do so in the view, I get the error.  So what am I doing wrong? 

    Monday, December 10, 2018 9:44 PM

All replies

  • User1520731567 posted

    Hi mj1223,

    According to your code,

    @model IEnumerable<Admin.Models.GetCustomJobPattern_Result>

    Your Edit view is strongly typed to a collection of GetCustomJobPattern_Result entity(IEnumerable<GetCustomJobPattern_Result>) ,

    and in your view you are executing code like Model.JobName (model=>model.JobName in Html helper methods),model.A1,model.A2...

    Here Model is a IEnumerable<GetCustomJobPattern_Result> and IEnumerable does not have these properties.

    Unless,add Index in IEnumerable<> or foreach,such as:

    @Model[0].JobName  @Model[1].JobName

    or

     @foreach (var item in Model)
    {
    @Html.LabelFor(modelItem=> item.JobName) }

    Else,instead of using a collection, Use a single instance of GetCustomJobPattern_Result as your model in your Edit view:

    @model Admin.Models.GetCustomJobPattern_Result

    And your result of your Edit Controller also need to be changed:

    For example:

    public ActionResult Edit(long? id)
    {
         ...
    //List<GetCustomJobPattern_Result> pattern = cp.JobPattern();

    GetCustomJobPattern_Result pattern = xxxxx....

    return View(pattern);
    ... } ... }

    Best Regards.

    Yuki Tao

    Tuesday, December 11, 2018 6:29 AM
  • User1520731567 posted

    Hi mj1223,

    Is this part of the code error?

    foreach(var item in pattern)
            {
                ViewBag.JobName = item.JobName;
            }

    As I can see in your code:

     List<GetCustomJobPattern_Result> pattern = cp.JobPattern();
     public List<GetCustomJobPattern_Result> JobPattern()
        { 
            return db.GetCustomJobPattern(JobID, SecAccount).ToList();
        }

    This line:

    db.GetCustomJobPattern(JobID, SecAccount).ToList();

    Does this line display the collection which just contains two fields (JobID and SecAccount)?

    If so,I suggest you could add JobName in this line,if this is your custom method:

    db.GetCustomJobPattern(JobID, SecAccount,JobName ).ToList();

    Best Regards.

    Yuki Tao

    Thursday, December 20, 2018 8:30 AM
  • User-2012457684 posted

    No.  JobPattern does return a list, even though it is a single record.

    Here are the models 

     public partial class GetCustomJobPattern_Result
        {
            public string JobName { get; set; }
            public string A1 { get; set; }
            public string A2 { get; set; }
            public string A3 { get; set; }
            public string A4 { get; set; }
            public string A5 { get; set; }
            public string A6 { get; set; }
            public string A7 { get; set; }
            public string D1 { get; set; }
            public string D2 { get; set; }
            public string D3 { get; set; }
            public string D4 { get; set; }
            public string D5 { get; set; }
            public string D6 { get; set; }
            public string D7 { get; set; }
            public string D8 { get; set; }
            public string D9 { get; set; }
            public string D10 { get; set; }
        }
    
    public class CustomPattern
    {
        JobDatabaseEntities db = new JobDatabaseEntities();
        public string SecAccount { get; set; }
        public long JobID { get; set; }
        public List<GetCustomJobPattern_Result> JobPattern()
        { 
            return db.GetCustomJobPattern(JobID, SecAccount).ToList();
        }
    }

    All interaction with the database is via stored procedure.  There are no LINQ queries in the system as SELECT, INSERT, UPDATE and DELETE are not available to users coming in from the web. Our databases are all locked down to prevent hacking attacks.   For most people it would be results from something like this

    Custom custom = db.custom.Where(q => q.JobID == JobID && q.SecAccount  == SecAccount)  /* we actually only need to pass the JobID to get the pattern, secondary account is also passed to prevent someone from altering the querystring and altering a pattern that is not theirs */


    I don't need to pass the JobName, besides which, at this point we do not even know what it is.    That is returned as one of the items in the results.

    I am not sure how to get a single record's results as a class

    Thursday, December 20, 2018 9:19 PM
  • User1520731567 posted

    Hi mj1223,

    I am not sure how to get a single record's results as a class

    No need to explain too much in model.

    I think you could refer to one to many entity in mvc:

    For example:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Grade Grade { get; set; }
    }
    
    public class Grade
    {
        public int GradeID { get; set; }
        public string GradeName { get; set; }
        public string Section { get; set; }
        
        public ICollection<Student> Student { get; set; }
    }

    Best Regards.

    Yuki Tao

    Friday, December 21, 2018 8:36 AM
  • User753101303 posted

    Hi,

    If you expect one element you could use

    Custom custom = db.custom.Single(q => q.JobID == JobID && q.SecAccount  == SecAccount)

    You'll have also to change to @model Admin.Models.GetCustomJobPattern_Result and then Model.JobName sould work (for now you are trying to call IEnumerable<Admin.Models.GetCustomJobPattern_Result>.JobName  which doesn't exists.

    Friday, December 21, 2018 9:09 AM