$('button').click(function () {
[1, 2, 3, 4, 5].forEach(function (n) {
if (n == 3) {
// it should break out here and doesn't alert anything after
return false
}
alert(n)
})
})Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>Run Code Online (Sandbox Code Playgroud)
我的问题:虽然我打电话,为什么它仍会提醒下一个号码return?就像:忽略下面的代码并继续下一个元素
我已经定义了一个临时变量来获取当前用户ID,它总是返回null.
这是快照:
为什么?
更新:
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return Json(new { success = false, ex = "Fail to login." });
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, isPersistent: true, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
string userId = User.Identity.GetUserId();
return Json(new { success = true });
case SignInStatus.Failure:
return Json(new { success = false, ex = "Email or password was incorrect." });
default:
return Json(new { …Run Code Online (Sandbox Code Playgroud) 我正在编写一个小代码来更好地理解property和static property.像这些:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; set; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
Run Code Online (Sandbox Code Playgroud)
要么
class UserIdentity
{
public IDictionary<string, DateTime> OnlineUsers { get; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
Run Code Online (Sandbox Code Playgroud)
自从我改为:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
Run Code Online (Sandbox Code Playgroud)
它给了我错误信息:
无法将属性或索引器"UserIdentity.OnlineUsers"分配给 - 它是只读的
我知道属性OnlineUsers是read only …
我的情况如下:
模型:
public class Book
{
public string Id { get; set; }
public string Name { get; set; }
}
public class Comment
{
public string Id { get; set; }
public string BookId { get; set; }
public string Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public IActionResult Detail(string id)
{
ViewData["DbContext"] = _context; // DbContext
var model = ... // book model
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
视图:
详细视图:
@if (Model?.Count > 0)
{
var context = (ApplicationDbContext)ViewData["DbContext"];
IEnumerable<Comment> …Run Code Online (Sandbox Code Playgroud) 我的情况:
public class A
{
public string _prop { get; }
public A(string prop)
{
_prop = prop; // allowed
}
}
Run Code Online (Sandbox Code Playgroud)
另一个案例:
public class A
{
public string _prop => string.Empty;
public A(string prop)
{
// Property or indexer 'A._prop' cannot be assigned to -- it is read only
_prop = prop;
}
}
Run Code Online (Sandbox Code Playgroud)
两种语法:
public string _prop { get; }
Run Code Online (Sandbox Code Playgroud)
和
public string _prop => string.Empty;
Run Code Online (Sandbox Code Playgroud)
创建一个只读属性.但是为什么我不能在第二种情况下分配呢?
我创建了一个带有个人用户帐户身份验证的 Web 应用程序 MVC 项目。
之后我创建了一个自定义的 UserIdentity。我正在尝试更改身份的默认“注册”视图,添加我的新字段,但是我只能将新页面搭建为 Razor 页面。
我的问题是:是否有可能在像 MVC 这样的结构中搭建这些新视图?我根本不想将 MVC 与 Razor Pages 混合使用。
如果这是不可能的,有没有办法创建一个覆盖默认行为的控制器(例如身份/帐户/注册)?
我正在编写一个小示例来练习创建类的新实例。
我有以下代码:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class MainClass
{
static void Main()
{
var p = new Person
{
Name = "Harry",
Age = 20
};
Console.WriteLine($"Name: {p.Name}. Age: {p.Age}");
p = new Person()
{
Name = "Hermonie",
Age = 18
};
Console.WriteLine($"Name: {p.Name}. Age: {p.Age}");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
它正在工作。
我的问题:有什么区别
var p = new Person {};
Run Code Online (Sandbox Code Playgroud)
和
var p = new Person() {};
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个版本?
来自MSDN doc:
public bool Contains(
string value
)
Run Code Online (Sandbox Code Playgroud)
返回值:如果value参数出现在此字符串中,或者value为空字符串(""),则返回true ; 否则,错误.
异常:ArgumentNullException:value为null.
例:
string s = string.Empty; //or string s = "";
Console.WriteLine(s.Contains("Hello World!")); //output: False
Run Code Online (Sandbox Code Playgroud)
如果我将其更改为:
try
{
string s = null; //or string s;
Console.WriteLine(s.Contains("Hello World!"));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Run Code Online (Sandbox Code Playgroud)
它会抛出一个错误信息:Object reference not set to an instance of an object因为string没有默认值(如""从)默认值表(C#参考),
请回到示例,如果我声明代码将工作s:
string …Run Code Online (Sandbox Code Playgroud) 我的测试代码:
using (var db = new MyDbContext())
{
string fromUser = ""; //sender
string toUser = ""; //receiver
var messages = db.Message.Where(x => x.FromUser == fromUser && x.ToUser == toUser)
.ToList();
foreach (var message in messages)
{
message.IsRead = true;
db.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:foreach在这种情况下,是否有另一种更新数据库而不使用循环的方法?
p/s:我一直在提到这些问题,但所有问题都没有解决我的问题:
我正在编写一个小例子来调用函数getAge()来通过对象显示年龄.
我有3种方法可以做到这一点:
"use strict";
var personClass = class {
static getAge() {
return 18
}
};
var personJSON = {
getAge: function () {
return 18
}
};
function personFunc() {
var getAge = function () {
return 18
};
// set a flag to point to getAge function
this.getAge = getAge()
}
$('#btn1').click(function () {
alert(personClass.getAge())
});
$('#btn2').click(function () {
alert(personJSON.getAge())
});
$('#btn3').click(function () {
var p = new personFunc();
alert(p.getAge)
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn1">Get age …Run Code Online (Sandbox Code Playgroud)c# ×6
asp.net-core ×2
asp.net-mvc ×2
javascript ×2
.net ×1
arrays ×1
asp.net ×1
c#-6.0 ×1
ecmascript-6 ×1
identity ×1
linq ×1
razor ×1
string ×1