如何在请求字段的视图中获取另一个模型属性

问题描述 投票:0回答:0

如何获取视图中asp-for中的coursename文本框值。当然是与外键链接的不同模型。我在使用 asp-for="CourseName" 时出现编译错误

public partial class Student
{
    public int StdId { get; set; }
    public string? StdName { get; set; }
    public virtual ICollection<StudentCourse> StudentCourses { get; } = new List<StudentCourse>();
}

public partial class StudentCourse
{
    public int StdId { get; set; }
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public virtual Student? StdId { get; set; }
 }
view
<input type="text" class="form-control" asp-for="CourseName" id="CourseName" name="CourseName">

controller
public IActionResult Save(IFormCollection formdata)
{
var coursedetails = formdata["CourseName"];
}

public partial class Student
{
    public int StdId { get; set; }
    public string? StdName { get; set; }
    public virtual ICollection<StudentCourse> StudentCourses { get; } = new List<StudentCourse>();
}

public partial class StudentCourse
{
    public int StdId { get; set; }
    public int CourseId { get; set; }
public string CourseName { get; set; }
    public virtual Student? StdId { get; set; }
 }
view
<input type="text" class="form-control" asp-for="CourseName" id="CourseName" name="CourseName">

controller
public IActionResult Save(IFormCollection formdata)
{
var coursedetails = formdata["CourseName"];
}
c# asp.net-mvc view model
© www.soinside.com 2019 - 2024. All rights reserved.