小编Phi*_*ger的帖子

获取Asp.Net MVC 5中登录用户的UserID

我是ASP.Net MVC的新手,现在尝试使用内置的用户登录功能.我可以在注册视图中注册用户.如果我尝试使用创建的用户登录,这也有效.我被重定向到母版页.

但是我无法获得当前用户的UserID.我在HomeController和AccountController中尝试了我的代码,但两者都没有用.第一行中的语句返回null.

var userID = User.Identity.GetUserId();

if (!string.IsNullOrEmpty(userID))
{
    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext.Create()));
    var currentUser = manager.FindById(User.Identity.GetUserId());
}
Run Code Online (Sandbox Code Playgroud)

在获取UserID之前,我是否需要做其他事情?

c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

41
推荐指数
1
解决办法
7万
查看次数

在WPF中使用MVVM将n个矩形添加到画布

我想在我的mvvm应用程序的主窗口中添加一组矩形.在我的viewModel中,我有一个对象集合,我用转换器转换为System.Windows.Shapes.Rectangle类(代码如下):

视图模型:

RecognizedValueViewModel 
{
    public ObservableCollection<BarcodeElement> BarcodeElements
    {
        get { return _BarcodeElements; }
        set { _BarcodeElements = value; }
    }

    public RecognizedValueViewModel()
    {
        BarcodeElements = InitializeBarcodeElements();
    }
}
Run Code Online (Sandbox Code Playgroud)

转换器:

public BarcodeElementToRectangleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Rectangle barcodeRectangle = GetRectangleFromBarcodeElement(value as BarcodeElement);

        return barcodeRectangle;
    }
}
Run Code Online (Sandbox Code Playgroud)

矩形应显示在MainWindow的画布中:

<Canvas x:Name="Canvas_Image_Main">
    <!-- Show rectangles here -->
</Canvas>
Run Code Online (Sandbox Code Playgroud)

我会在代码中将Rectangles添加到canvas中,但我现在还没有在运行时有多少个矩形.我有办法实现这个目标吗?保护你.

c# wpf user-interface canvas mvvm

17
推荐指数
2
解决办法
2万
查看次数

使用 DocumentFormat.OpenXML SDK 编写文字表单字段

我正在编写一个应用程序,该应用程序应使用 DocumentFormat.OpenXML SDK 来写入数据以形成 Word 模板中的字段。但我在存储表单字段的 SDK 文档对象中找不到属性。

我尝试了这段代码:

using (WordprocessingDocument document = WordprocessingDocument.Open("Path/To/document.dotx", true))
{
    document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
    MainDocumentPart mainPart = document.MainDocumentPart;

    var fields = mainPart.Document.Body.Descendants<FormFieldData>();

    foreach (var field in fields)
    {
        if (field.GetType() == typeof(FormFieldData))
        {
            if (field.LocalName == "Name")
            {
                Console.WriteLine("Hi!");
            }   
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

但字段始终为空。

c# ms-word openxml openxml-sdk

6
推荐指数
1
解决办法
5881
查看次数

通用参数的动态长度

我编写了一个获取对象数组的方法,将数组内容的类型作为泛型,并尝试将每种类型转换为传递的泛型:

    public static void GetMultipleObjectsFromParameters<T, U>(
        object parameterArray, out T parameter1, out U parameter2)
    {
        parameter1 = default(T);
        parameter2 = default(U);

        try
        {
            object[] arr = parameterArray as object[];

            if (arr == null)
            {
                Debug.WriteLine("array not valid");
                return;
            }
            if (arr.Length != 2)
            {
                Debug.WriteLine("arr.Length != 2");
                return;
            }

            parameter1 = (T)arr[0];
            parameter2 = (U)arr[1];
        }
        catch (Exception ex)
        {
            Debug.Write(ex);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我认为如果我使用BackgroundWorker并希望提供不同类型的多个参数(例如,第一个参数为字符串,第二个为整数...),此方法可能非常有用.

现在我只是想知道是否有办法在不强制固定大小的参数的情况下编写此方法.这将阻止我为每个参数计数编写这样的方法.

我希望这个问题是可以理解的.有一个简单的方法吗?谢谢您的帮助.

.net c# generics

1
推荐指数
1
解决办法
232
查看次数

For循环使用布尔值而不是变量初始化

我有这个代码片段:

private static int counter = 0;

public static void main(String[] args) {
    int x = 1;

    for (test('1'); test('2') && (x <= 2); test('3'))
    {
        x++;
        test('4');
    }

    System.out.println(" Counter=" + counter);
}

static boolean test(char num)
{
    System.out.print(" " + num);
    counter++;
    return true;
}
Run Code Online (Sandbox Code Playgroud)

为什么for循环在这里?我认为for循环中的第一个语句必须是变量的初始化,并且这不会编译,但它会运行并输出输出:

 1 2 4 3 2 4 3 2 Counter=8
Run Code Online (Sandbox Code Playgroud)

这是boolean做什么的?

java variables for-loop boolean

1
推荐指数
1
解决办法
163
查看次数

从另一个类在 ASP.Net MVC 控制器中设置会话

我正在尝试从另一个控制器访问我的 MVC HomeController 的方法,以将用户重定向到主页(如果用户未通过身份验证)。问题是,如果我自己实例化 HomeController 中的 Session,则不会设置它,但我必须从 HomeController 访问存储在会话中的变量。我尝试这样解决问题:

HomeController currentHomeController = new HomeController();
currentHomeController.Session = this.Session;
return currentHomeController.Index();
Run Code Online (Sandbox Code Playgroud)

但是 Session 变量没有设置器,所以这不起作用。有其他方法可以做到这一点吗?

我的解决方案

这有效:

 return RedirectToAction("Index", "Home");
Run Code Online (Sandbox Code Playgroud)

感谢您的回答!

c# asp.net asp.net-mvc asp.net-mvc-4

1
推荐指数
1
解决办法
2万
查看次数

隐式类型转换 - Int为double

我有一个大学的练习,看起来像:

int a = 10;
int b = 3;

double c = a / b;
Run Code Online (Sandbox Code Playgroud)

问题是:哪个值是c.

现在我想说,c是3.3.在计算结果之前,它被隐式转换为double.但是这个问题的正确答案是根据我的记录3.0.

怎么会这样?编译器是否真的首先将结果计算为整数,然后在第二步中将其转换为加倍?

或者我是否理解错误?

java casting implicit-conversion

0
推荐指数
1
解决办法
250
查看次数

Knockout-Binding仅适用于One-Way

我正在使用knockout with typescript来构建我的应用程序的UI.如果我的UI中的某些内容发生变化,我想使用敲除绑定来通知我的打字稿.

我的代码有效,但不幸的是只有单向.加载页面后显示初始值5,但如果我在breakTime输入中更改了某些内容,则永远不会输入subscribe-method.

selectedDates数组包含包含breakTime属性的对象.

HTML代码:

<!-- ko 'foreach': $root.selectedDates-->
<td>
    <input type="text" name="breakTime" data-bind="value: breakTime()" class="bookingTextBox" />
</td>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)

打字稿代码:

public breakTime: KnockoutObservable<string>;

constructor(date: Date, viewModel: Booking.BookingViewModel) {    
    this.breakTime = ko.observable("30");
    this.breakTime.subscribe((newValue) => {
        alert(newValue);
    });
}
Run Code Online (Sandbox Code Playgroud)

我以前从未使用过淘汰的foreach-element,也许错误是由此引起的.我是否必须做其他事情才能使这个工作?

html javascript knockout-2.0 knockout.js typescript

0
推荐指数
1
解决办法
275
查看次数

将时间跨度添加到另一个时间跨度不起作用

我有两个时间跨度,我想在第一个时间点添加第二个时间跨度:

TimeSpan weeklyWorkTimeHours = new TimeSpan(0,0,0);
TimeSpan? completeWorkTimeForCurrentDay = 
CalculateCompleteWorktime(currentWorkTimeItem).Value; /* I debugged through 
the code. This method returns a correct timespan with a correct value */
weeklyWorkTimeHours.Add(completeWorkTimeForCurrentDay.Value);
Run Code Online (Sandbox Code Playgroud)

但即使在最后一行代码之后,weeklyWorkTimeHours也包含0,0,0.为什么不在此上下文中添加工作?

.net c# datetime timespan

0
推荐指数
1
解决办法
499
查看次数