0

I just want to select some specific columns from a table using LINQ or lambda expressions and then send the table to the client using Tempdata. But it's not working for me.

my controller =>(using LINQ)

TempData["Courses"] = (from a in db.Courses select new { a.name, a.vendor_heading }).ToList();

and using lambda expression=>

TempData["Courses"] = db.Courses.Select(x => new { x.name, x.vendor_heading }).ToList();

It seems perfect for me but I don't know why it's not working. but if I use =>

TempData["Courses"] = db.Courses.ToList();

It's absolutely working for me. (above I try to select * from Table) when I try to send the full table, it's not having any issue. But if I want to send a specific column, I have the issue. I need to send specific column => because I need only those columns.

In my view side=>

<div>
     @Html.LabelFor(model => model.Course.vendor_heading, "Vendor Name", new { @class = "control-label" })
     @{
       var VendorName = CoursesTable.Where(x => x.name == Model.name).Select(x => x.vendor_heading).First();
      }
     @Html.TextBox("vendor_heading", VendorName, new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Course.vendor_heading), @autocomplete = "off", @readonly = "readonly" })
</div>

@{
IEnumerable<Course> CoursesTable = TempData["Courses"] as   IEnumerable<Course>;
 }

I mentioned above that if I send specific column it gives me error like=> enter image description here my model=>

public class Course
{
    [MaxLength(700, ErrorMessage = "The Max Length For Heading Is 700 Character!")]
    [Required(ErrorMessage="Vendor Name Is Required!")]
    [Display(Name = "Vendor Heading")]
    public string vendor_heading { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Key]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Course Name Is Required!")]
    [MaxLength(700, ErrorMessage = "The Max Length For Course Name Is 700 Character!")]
    [Display(Name = "Course Name")]
    public string name { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Course Code Is Required!")]
    [MaxLength(200, ErrorMessage = "The Max Length For Course Code Is 200 Character!")]
    [Display(Name = "Course Code")]
    public string code { get; set; }
    /// <summary>
    /// //////////
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Picture Is Required!")]
    [MaxLength(1000, ErrorMessage = "The Max Length For Pic Path Is 1000 Character!")]
    [Display(Name = "Pic")]
    public string pic_path { get; set; }
    /// <summary>
    /// /////////
    /// </summary>
    [Required(ErrorMessage = "Adding date Is Required!")]
    [DataType(DataType.Date, ErrorMessage = "Invalid Date!")]
    [Display(Name = "Adding Date")]
    public DateTime adding_date { get; set; }
    /// <summary>
    /// /////////
    /// </summary>
    [MaxLength(5000, ErrorMessage = "The Max Length For Course Details Is 5000 Character!")]
    [Display(Name = "Course Details")]
    public string details { get; set; }

    //relationship With  other tables-------
    [ForeignKey("vendor_heading")]
    public Vendor Vendor { get; set; }
    public List<Batche> Batches { get; set; }
}

I don't know if I could explain my problems to you guys properly. If not, let me know and if did, please help me....

4
  • Where does CoursesTable come from? Commented May 15, 2017 at 19:14
  • @Simon i edited my details ..please have a look again.... Commented May 15, 2017 at 19:24
  • If you are going to use it in the View, you will need to add it to the ViewBag or ViewData Commented May 15, 2017 at 19:26
  • @Simon yah i add a temp data .and if i not not wrong this is also a way? Commented May 15, 2017 at 19:30

2 Answers 2

1

In your controller get the vendor name you want to display:

ViewBag["VendorName"] = db.Courses.Where(x => x.name == Model.name).Select(x => x.vendor_heading).First();

Then in your View use

@Html.TextBox("vendor_heading", ViewBag["VendorName"], new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Course.vendor_heading), @autocomplete = "off", @readonly = "readonly" })
Sign up to request clarification or add additional context in comments.

Comments

0

As you said , it should work with tempdata also.

Try with the following change:

Thats, in your code itself put the casting code ,before the line, where the error occurs, because this is the order in which the code will be executed.

 @{
      if(TempData["Courses"] != null)
{
    IEnumerable<Course> CoursesTable = TempData["Courses"] as   IEnumerable<Course>;
}
     }




    <div>
         @Html.LabelFor(model => model.Course.vendor_heading, "Vendor Name", new { @class = "control-label" })
         @{
           var VendorName = CoursesTable.Where(x => x.name == Model.name).Select(x => x.vendor_heading).First();
          }
         @Html.TextBox("vendor_heading", VendorName, new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Course.vendor_heading), @autocomplete = "off", @readonly = "readonly" })
    </div>

Hope this also will work for you as you thought,

kindly let me know your feedbacks

Thanks karthik

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.