显示模型中的单个字段


public class StudentViewModel
    {
        public Student studentVm { get; set; }
        public StudentAdditionalInfo studentAdditionalInfoVm { get; set; }
        public int rcImgToProcess { get; set; }     
    }

studentVmstudentAdditionalInfoVm存储我的 2 个表的数据StudentStudentAdditionalInfo,这些表包含多条记录

rcImgToProcess存储我从控制器传递的记录计数的数据,因此它只是一个数据(我可以使用 ViewBag,但由于某种原因我更喜欢将其传递给 Model(

<div>
    I want the value of [rcImgToProcess] here
</div>
@foreach (var item in Model)
    {
        <tr>
            <td>@item.studentVm.Id  </td>
            <td>@item.studentVm.StudentCourse</td>
            <td>@item.studentAdditionalInfoVm.MotherName</td>
            <td>@item.studentAdditionalInfoVm.FatherName</td>
        </tr>
    }

控制器

int rcImgToProcess = "0";
rcImgToProcess= 1001;
var studentList = from s in student
    join st in studentAdditionalInfo on s.Id equals st.Id into st2
    from st in st2.DefaultIfEmpty()
    select new StudentViewModel {
       studentVm = s,
       studentAdditionalInfoVm = st,
       rcImgToProcess = rcImgToProcess
    };
return View(studentList);

如何调用rcImgToProcess显示在页面的标题部分

您是否正确引用了视图中的StudentViewModel

您应该在文件顶部有@model <namespaceForStudentViewModel>.StudentViewModel

例如@model SampleApplication.ViewModels.StudentViewModel

然后,您可以在视图中的任意位置使用 <div>@Model.rcImgToProcess</div>

最新更新