用户为admin时更改Guid变量

时间:2013-10-23 13:58:56

标签: c# razor

我正在构建一个快速原型应用程序,其中有两个硬编码用户,其中包含患者用户和管理员的测试数据。也可以创建新用户,但不会显示他们只能添加的任何数据。我有一种情况,我调用一个接收Guid作为参数的方法。当用户是管理员时,我想为现有的患者用户传入硬编码的Guid,如果不是管理员,我想将其基于附加到用户会话的Guid,下面是我的剃刀代码,目前无效。

Guid patientID = new Guid("3aac8d07-ad35-e311-8bdf-9ebf7757768f");

@if(userSession.IsAdmin == "TRUE")
{
    System.Data.DataSet ds = MeasuredHealthBeta1.Utilities.DataHelper.Measurements_Get306060DayGlucoseMeasurements(patientID );
}
else
{
    System.Data.DataSet ds = MeasuredHealthBeta1.Utilities.DataHelper.Measurements_Get306060DayGlucoseMeasurements(userSession.UserIDNative);
}

发生包含此剃刀代码的视图使用以下方法加载时出现“外部组件抛出异常”错误

 @{Html.RenderPartial("../Metrics/ReportsHistoryList", Model, new ViewDataDictionary(this.ViewData) { { "MeasurementTypeGroupIDs", defaultMeasurementTypeIDs } });}  

1 个答案:

答案 0 :(得分:0)

我得到的问题实际上是DataSet ds在if语句的范围之外使用。为了解决这个问题而不是用条件包装方法调用,我包装了我试图传入的Guid。这是我使用的代码,最终为我工作,它是脏/ hackish,我很可能也不需要那里的其他。

Guid patientID = userSession.UserIDNative;
if(userSession.IsAdmin.ToUpper() == "TRUE")
{
    patientID = new Guid("3aac8d07-ad35-e311-8bdf-9ebf7757768f");
}
else
{
    patientID = userSession.UserIDNative;
}

System.Data.DataSet ds = MeasuredHealthBeta1.Utilities.DataHelper.Measurements_Get306060DayGlucoseMeasurements(patientID);