asp.net-core-mvc

Bootstrap Datepicker: Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

荒凉一梦 提交于 2019-12-02 20:26:13
问题 I am a newbie here. I have gone through similar questions here but none of them helped. I have the following in my ViewModel : public DateTime FromDate { get; set; } public DateTime ToDate { get; set; } When I run the code, in the View after GET Method , I am getting the default dates as: 01/01/0001 , in other words, null /default value. I searched online and found out that I need to make these fields nullable . Therefore, I changed the above code to: public DateTime? FromDate { get; set; }

TagHelper for passing route values as part of a link

馋奶兔 提交于 2019-12-02 20:01:27
When specifying asp-controller and asp-action on a link, what's the syntax for also passing an id attribute? E.g. If I wanted to link to the edit URL for a given object, the required URL would be /user/edit/5 for example. Is there a method to achieve this using TagHelpers, or do we still have to fall back to @Html.ActionLink() ? You can use the attribute prefix asp-route- to prefix your route variable names. Example: <a asp-action="Edit" asp-route-id="10" asp-route-foo="bar">Edit</a> I'd like to suggest a combination of the other two answers, but with a bit of extra clarification. You will use

Posting files and model to controller in ASP.NET Core MVC6

大兔子大兔子 提交于 2019-12-02 19:35:32
I'm migrating a project from ASP.NET RC1 to ASP.NET Core 1.0. I have a view that allows users to upload one of more files, which I post using Jquery Ajax. I also serialize and post some settings within the same post. The following all worked in RC1 (and pre-asp.net core): Js: $('#submit').click(function () { var postData = $('#fields :input').serializeArray(); var fileSelect = document.getElementById('file-select'); var files = fileSelect.files; var data = new FormData(); for (var i = 0; i < files.length; i++) { data.append('file' + i, files[i]); } $.each(postData, function (key, input) { data

Run a background task from a controller action in asp.net core 2

断了今生、忘了曾经 提交于 2019-12-02 19:07:01
I am developing a web application with a REST Api using C# with asp.net core 2.0 What I want to achieve is when the client send a request to an endpoint I will run a background task separated from the client request context which will be ended if the task started successfully. I know there is HostedService but the problem is that the HostedService starts when the server starts, and as far as I know there is no way to start the HostedService manually from a controller. Here is a simple code that demonstrate the question. [Authorize(AuthenticationSchemes = "UsersScheme")] public class

How to protect a Web API using ASP.NET 5 MVC 6

爱⌒轻易说出口 提交于 2019-12-02 19:05:40
I have a nice ASP.NET 5 / MVC 6 app up and running. Essentially for this purpose it is just the normal sample app you get when you start a new project to keep it simple. So far I can: Register a user Login Logout Protect a page (forcing login etc) Now, what I would like is to provide an API mechanism for a app to login and get an authentication token. Specifically I am working on two mobile apps to test with, one using Angular / Cordova and one using Xamarin. I have looked high and low and I cannot seem to find an example yet that shows how to make this work. Every example I find so far

How to add Roles to Windows Authentication in ASP.NET Core

假如想象 提交于 2019-12-02 18:55:16
I created an asp.net core project in visual studio 2015 with windows authentication. I can't figure out how to add roles to the Identity. I have a table with usernames for the windows account. And when the user opens the website the user is added to the Identity (I assume that's what happens, because I can display the username by User.Identity.Name) and I want to pull out Roles from another table and assign them to the user, is this possible? Or perhaps is there a better way to do it? (Why?, How?) I couldn't find any examples specific examples related to windows authentication, but I have read

Using enum for dropdown list in ASP.NET MVC Core

為{幸葍}努か 提交于 2019-12-02 18:42:35
I'm trying to create a dropdown list with an enum property in ASP.NET MVC Core using the tag helper in a Razor view: Here is the model: public class PersonalMember : Member { [Required, Display(Name = "First Name")] public string FirstName { get; set; } [Required, Display(Name = "Last Name")] public string LastName { get; set; } [EnumDataType(typeof(Gender))] public Gender GenderType { get; set; } } public enum Gender { Male = 1, Female = 2 } Here is part of a form in the view: <div class="form-group"> <label asp-for="GenderType" class="col-md-2 control-label"></label> <div class="col-md-10">

Any samples for mvc6 with signalr?

三世轮回 提交于 2019-12-02 18:16:42
I have a full-working ASP.NET MVC application (.NET Core, ASP.NET MVC 6) . I would now like to add a signalr to my application. Any samples for MVC with signalr ? The MusicStore sample on GitHub use signalR and the BugTracker sample also and you have a sample in the SignalR-Server repo Or you can look at my sample on GitHub Luke T O'Brien I don't believe there are any yet. The next version of SignalR, version 3, is designed for ASP.NET Core, SignalR 2 doesn't support this. If you look on the ASP website https://www.asp.net/learn it doesn't mention that you can use SignalR with ASP.NET Core .

How to set .NET Core in #if statement for compilation

断了今生、忘了曾经 提交于 2019-12-02 17:59:52
I created a multi targeted framework project. I use something like this : #if NET40 Console.WriteLine("hello from net 4"); #endif But I cant find wildcard for .NET Core. I tried : #if NETCOREAPP1.0 Console.WriteLine("hello from net Core"); #endif but it is not valid statement. Thanks. Ralf Bönning You need underscore _ instead of point : NETCOREAPP1_0 or the more recent NETCOREAPP1_1 and NETCOREAPP2_0 The article https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/libraries includes a list for the different preprocessor symbols. .NET Framework 2.0: NET20 .NET Framework 3.5: NET35

Data Annotation for currency format not working

旧巷老猫 提交于 2019-12-02 17:43:31
问题 In my ASP.NET MVC Core web project on VS2015 , the following model is displaying data as, e.g., 15481 instead of $15,481 even though I'm using [DisplayFormat] below: Models : public class State { [Key] public int StateId { get; set; } [Column(TypeName ="varchar(40)")] public string StateName { get; set; } [Column(TypeName = "char(2)")] public string StateCode { get; set; } } public class Sales { [Key] public int SalesId { get; set; } public int? FiscalYear { get; set; } [DisplayFormat