MVC C#从控制器方法访问URL参数

时间:2018-01-05 09:08:28

标签: c# asp.net-mvc methods parameters

我正在使用C#开发Asp.net MVC项目,这是我的第一个MVC项目,所以我仍然缺少基本的细节。

在我的某个网页上使用超链接时,如下所示:

<a href="\ScheduleDetails\Index\@item.workScheduleId">
           @Html.DisplayFor(modelItem => item.scheduleName)
        </a>

然后,ScheduleDetails控制器内的索引方法将是:

public ActionResult Index(long id)
{
    var scheduledetails = db.scheduleDetails.Where(s =>s.workScheduleId==id);
    this.workScheduleId = id;
    return View(scheduledetails.ToList());
}

请注意,我已将变量workScheduleId定义为控制器中的实例私有变量:

    private ScheduleDetailsDbContext db = new ScheduleDetailsDbContext();
    private long workScheduleId;

现在在调试时,index方法正确读取参数,并将其正确分配给私有变量workScheduleId。但是当我试图从同一控制器内的Create方法访问workScheduleId时,它返回0!虽然我确定变量在index方法中被赋值为正确的值,但为什么在调用Create方法时这个值会丢失?如何从Create方法或任何其他方法访问参数?

0 个答案:

没有答案