如何从控制器保留 html 中的变量

本文关键字:变量 html 保留 控制器 | 更新日期: 2023-09-27 18:31:54

我的项目中有一个控制器

public ActionResult Create(int PeriodID = 0)
    {
        var ParentQry = from d in db.Periods
                        join sc in db.MultiRaterSurveyPeriods on d.PeriodID equals sc.PeriodID
                        where d.StartDate < DateTime.Now && d.EndDate > DateTime.Now && sc.StartDate < DateTime.Now && sc.EndDate > DateTime.Now
                        orderby d.PeriodID
                        select new {PeriodID = sc.MultiRaterSurveyPeriodID, PeriodName = d.PeriodName };
        var GroupResp = from d in db.MultiRaterSurveyGroups
                        join sc in db.RelationBetweenMRSPeriodMRSGroups on d.MultiRaterSurveyGroupID equals sc.MultiRaterSurveyGroupID
                        where d.StartDate < DateTime.Now && d.EndDate > DateTime.Now && sc.StartDate < DateTime.Now && sc.EndDate > DateTime.Now
                        orderby d.MultiRaterSurveyGroupID
                        select new { MultiRaterSurveyGroupID = d.MultiRaterSurveyGroupID, MultiRaterSurveyGroupName = d.MultiRaterSurveyGroupName, MultiRaterSurveyPeriodID = sc.MultiRaterSurveyPeriodID };
        ViewBag.PeriodID = new SelectList(ParentQry, "PeriodID", "PeriodName");
        ViewBag.surveygroups = new SelectList(GroupResp, "MultiRaterSurveyGroupID", "MultiRaterSurveyGroupName");}

如何从控制器获取变量以放入ViewBag.surveyperiod的create.cshtml中?

@foreach (var p in ViewBag.surveygroups)
                    {           
                                <input type="hidden" class="hiddenSurveyGroups" name="hiddenSurveyGroups" id="surveygroups" value="@ViewBag.surveygroups">
                    }

因为我希望这个值能得到javascript。

如何从控制器保留 html 中的变量

您可以将 p.Text 和 p.Value 用于 ViewBag.surveygroups。通过这个,您可以访问它。

在您的情况下,您可以将value="@ViewBag.surveygroups"替换为value="@p.Value"