gpt4 book ai didi

mysql - 使用 LINQ/lambda .net MVC 从表中选择某些列的所有数据?

转载 作者:行者123 更新时间:2023-11-29 10:49:31 25 4
gpt4 key购买 nike

我只想使用 LINQ 或 lambda 表达式从表中选择一些特定列,然后使用 Tempdata 将表发送到客户端。但这对我不起作用。

我的 Controller =>(使用 LINQ)

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

并使用 lambda 表达式=>

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

这对我来说似乎很完美,但我不知道为什么它不起作用。但如果我使用 =>

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

这对我来说绝对有效。 (上面我尝试从表中选择*)当我尝试发送完整的表格时,没有任何问题。但如果我想发送特定的专栏,就会遇到问题。我需要发送特定列 => 因为我只需要这些列。

在我看来=>

<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>;
}

我上面提到,如果我发送特定列,它会给我错误,例如=> enter image description here我的模型=>

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; }
}

我不知道我是否可以向你们正确解释我的问题。如果没有,请告诉我,如果有,请帮助我......

最佳答案

在您的 Controller 中获取您想要显示的供应商名称:

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

然后在您的 View 中使用

@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" })

关于mysql - 使用 LINQ/lambda .net MVC 从表中选择某些列的所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com