从模型中显示单个字段

时间:2018-01-14 03:25:43

标签: c# asp.net-mvc

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

        public int rcImgToProcess { get; set; }     
    }

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

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

<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显示在页面的标题部分

1 个答案:

答案 0 :(得分:0)

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

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

例如@model SampleApplication.ViewModels.StudentViewModel

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