我试图从html DIV元素检索Boolean类型的数据属性,但是当字符串转换为boolean时,它总是返回false.
HTML
<div id='test' data-return="true"></div>
Run Code Online (Sandbox Code Playgroud)
JS
isreturn_str = $('#test').data('return');
isreturn = (isreturn_str === 'true');
if (isreturn) {
document.write("It is true");
} else {
document.write("It is false");
}
Run Code Online (Sandbox Code Playgroud)
产量
这是假的
几天前,我遇到了ASP.Net线程的这个问题.我希望每个Web请求都有一个单例对象.我的工作单位实际上需要这个.我想为每个Web请求实例化一个工作单元,以便身份映射在整个请求中有效.这样我就可以使用IoC透明地将我自己的IUnitOfWork注入到我的存储库类中,并且我可以使用相同的实例来查询然后更新我的实体.
由于我使用Unity,我错误地使用了PerThreadLifeTimeManager.我很快意识到ASP.Net线程模型不支持我想要实现的目标.基本上它使用theadpool并回收线程,这意味着每个线程我得到一个UnitOfWork!但是,我想要的是每个Web请求的一个工作单元.
一些谷歌搜索给了我这个伟大的帖子.这正是我想要的; 除了非常容易实现的统一部分.
这是我对PerCallContextLifeTimeManager实现统一的实现:
public class PerCallContextLifeTimeManager : LifetimeManager
{
private const string Key = "SingletonPerCallContext";
public override object GetValue()
{
return CallContext.GetData(Key);
}
public override void SetValue(object newValue)
{
CallContext.SetData(Key, newValue);
}
public override void RemoveValue()
{
}
}
Run Code Online (Sandbox Code Playgroud)
当然,我使用它来使用与此类似的代码注册我的工作单元:
unityContainer
.RegisterType<IUnitOfWork, MyDataContext>(
new PerCallContextLifeTimeManager(),
new InjectionConstructor());
Run Code Online (Sandbox Code Playgroud)
希望能节省一些时间.
我已经为ASP.NET MVC Web应用程序编写了我的第一个单元测试.一切正常,它给了我有价值的信息,但我不能在视图模型中测试错误.ModelState.IsValid始终为true,即使未填充某些值(空字符串或null).
我已经读过,当发布的数据映射到模型时,模型验证会发生,您需要编写一些代码来自行进行模型验证:
我已经尝试了链接网页中提供的三个示例,但它似乎对我不起作用.
一些代码:
我的观点模型
...
[Required(ErrorMessageResourceName = "ErrorFirstName", ErrorMessageResourceType = typeof(Mui))]
[MaxLength(50)]
[Display(Name = "Firstname", ResourceType = typeof(Mui))]
public string FirstName { get; set; }
...
Run Code Online (Sandbox Code Playgroud)
控制器
...
[HttpPost]
public ActionResult Index(POSViewModel model)
{
Contract contract = contractService.GetContract(model.ContractGuid.Value);
if (!contract.IsDirectDebit.ToSafe())
{
ModelState.Remove("BankName");
ModelState.Remove("BankAddress");
ModelState.Remove("BankZip");
ModelState.Remove("BankCity");
ModelState.Remove("AccountNr");
}
if (ModelState.IsValid)
{
...
contractValidationService.Create(contractValidation);
unitOfWork.SaveChanges();
return RedirectToAction("index","thanks");
}
else
{
return Index(model.ContractGuid.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试
posViewModel.FirstName = null;
posViewModel.LastName = "";
...
var modelBinder = new ModelBindingContext() …Run Code Online (Sandbox Code Playgroud) 代码在 .NET Standard 2.0 中运行。我有一个构造函数调用一个方法,该方法将调用这样的 Azure 函数:
public ChatViewModel(IChatService chatService)
{
Task.Run(async () =>
{
if (!chatService.IsConnected)
{
await chatService.CreateConnection();
}
});
}
Run Code Online (Sandbox Code Playgroud)
方法是这样的:
public async Task CreateConnection()
{
await semaphoreSlim.WaitAsync();
if (httpClient == null)
{
httpClient = new HttpClient();
}
var result = await httpClient.GetStringAsync(uri);
var info = JsonConvert.DeserializeObject<Models.ConnectionInfo>(result);
//... some other code ...
semaphoreSlim.Release();
}
Run Code Online (Sandbox Code Playgroud)
代码停在
等待 httpClient.GetStringAsync(uri)
URI 是 100% 有效的,如果我将它复制并粘贴到浏览器中,我会得到我想要的 JSON。
打开 Fiddler 时,没有调用 URI。
编辑 源代码来自这个 Github 存储库:https : //github.com/PacktPublishing/Xamarin.Forms-Projects/tree/master/Chapter06-07/Chat/Chat
编辑 …
我试图抓住模式存储库的想法,并尝试在我已经设置的数据库结构中实现它.我现在正在努力获得使用查找表的最佳实践.我已经创建了一个测试项目,这是我的数据库模型:

您可以看到我有三个查找表:Lookup,Language和LookupLanguage.语言表只包含语言.

查找表包含整个模型中使用的不同类型.

LookupLanguage将两个表链接在一起:

我已经创建了一个新项目,所有模型都是1到1的数据库表:

我还创建了一个通用存储库和一个通用的CrudService接口:
public interface ICrudService<T> where T : IsActiveEntity, new()
{
int Create(T item);
void Save();
void Delete(int id);
T Get(int id);
IEnumerable<T> GetAll();
IEnumerable<T> Where(Expression<Func<T, bool>> func, bool showDeleted = false);
void Restore(int id);
}
Run Code Online (Sandbox Code Playgroud)
现在,根据以下帖子:在实现存储库模式时应该查找值/表获取自己的存储库吗?,存储库应隐藏底层数据库层.所以我认为我需要一个服务和/或存储库的新实现来获取查找,但是,我在哪里必须告诉我需要哪种语言进行查找?
让我们从公司的状态(新的,接受的,拒绝的)作为例子.
公司模式如下:
public partial class Company : IsActiveEntity
{
[Required]
[MaxLength(50)]
public string CompanyName { get; set; }
public System.Guid StatusGuid { get; set; }
[ForeignKey("StatusGuid")]
public virtual Lookup Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想我不需要单独实现存储库?但我需要一个单独的实现CompanyService.
interface …Run Code Online (Sandbox Code Playgroud) 按照我之前的问题(LINQ to Entities中仅支持无参数构造函数和初始值设定项)我仍然有一个问题.我只是想了解发生了什么以及为什么某些东西在一个案例而不是另一个案例中起作用.
如果要在Linq to entity查询中强制转换字符串参数(例如查询字符串参数),则必须使用新的Guid(request.querystring("param"))而不是Guid.parse(request.querystring("param") ")).Guid.parse将抛出异常,因为Linq无法将其转换为SQL.
我经常在我的代码中使用这种技术,它可以工作.
dim lstResult = DB.MyTable.Where(function(f) f.key = new Guid(request.querystring("param"))).toList()
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用Linq查询创建匿名类型时,它将抛出异常:
dim lstResult = DB.MyTable.Where(function(f) f.key = new Guid(request.querystring("param"))).Select(function(f) new With { .guid = f.guid, .name = f.name }).toList()
Run Code Online (Sandbox Code Playgroud)
抛出的异常是:
LINQ to Entities中仅支持无参数构造函数和初始值设定项
我可以(或应该)做的是预先声明Guid参数(这可能是一个好习惯),而不是在查询中使用它.它会工作:
dim myGuid = Guid.parse(request.querystring("param"))
dim lstResult = DB.MyTable.Where(function(f) f.key = myGuid).Select(function(f) new With { .guid = f.guid, .name = f.name }).toList()
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:为什么它会在没有创建匿名类型的情况下工作?为什么在尝试创建匿名类型时会引发异常?导致此异常的机制是什么?
我坚持以下情况.我有一个数据库,其中包含一个包含客户数据的表格和一个表格,我在其中放置记录以监控B2B网站上发生的情况.
客户表如下:
监测表:
PARAMETER1中包含客户指南以及存储的其他数据类型.
现在问题是根据他们上次访问日期订购我们的客户,最近访问过的客户必须排在网格顶部.
我正在使用实体框架,我遇到了比较字符串和guid类型的问题,所以我决定在监控表之上创建一个视图:
SELECT
ID,
CONVERT(uniqueidentifier, parameter2) AS customerguid,
USERguid,
CreationDate
FROM
MONITORING
WHERE
(dbo.isuniqueidentifier(parameter2) = 1)
AND
(parameter1 LIKE 'Customers_%' OR parameter1 LIKE 'Customer_%')
Run Code Online (Sandbox Code Playgroud)
我在EF中导入了视图并进行了Linq查询.它什么也没有返回,所以我提取了生成的SQL查询.在SQL Management Studio中测试查询时出现以下错误: 从字符串转换为uniqueidentifier时转换失败.
问题在于以下代码段(针对此问题进行了简化,但也出现了错误:
SELECT *,
(
SELECT
[v_LastViewDateCustomer].[customerguid] AS [customerguid]
FROM [dbo].[v_LastViewDateCustomer] AS [v_LastViewDateCustomer]
WHERE c.GUID = [v_LastViewDateCustomer].[customerguid]
)
FROM CM_CUSTOMER c
Run Code Online (Sandbox Code Playgroud)
但是当我加入时,我得到了我的结果:
SELECT *
FROM CM_CUSTOMER c
LEFT JOIN
[v_LastViewDateCustomer] v
on c.GUID = v.customerguid
Run Code Online (Sandbox Code Playgroud)
我试图制作一个SQL小提琴,但它正在该网站上工作.http://sqlfiddle.com/#!3/66d68/3
谁可以指出我正确的方向?
我正试图在网页上第一次实现knockoutjs.我偶然发现了以下问题,但也许这也是"最佳实践"的一个案例.
我有一个产品页面,产品可以有产品图像.当没有可用的产品图像时,该属性设置为null,我需要显示"无图像可用"图片.
我的模特:
function ProductOverview() {
var self = this;
self.guid = ko.observable();
self.Image = ko.observable();
self.IsActive = ko.observable(false);
}
Run Code Online (Sandbox Code Playgroud)
我的viewmodel:
function productOverviewModelView() {
var self = this;
self.productOverview = new ProductOverview();
self.ShowNoImage = ko.computed(function () {
if (self.productOverview.Image() === null || self.productOverview.Image() === "") {
return true;
} else {
return false;
}
}, this);
self.ImageAvailable = ko.computed(function () {
if (self.productOverview.Image() === null || self.productOverview.Image === "") {
return false;
} else {
return true;
}
}, this); …Run Code Online (Sandbox Code Playgroud) 如何重新配置 RabbitMQ 不使用 %appdata% 作为配置文件?
c# ×3
asp.net ×1
asp.net-mvc ×1
async-await ×1
html5 ×1
javascript ×1
jquery ×1
knockout.js ×1
lifetime ×1
modelstate ×1
rabbitmq ×1
singleton ×1
sql ×1
sql-server ×1
unit-of-work ×1
unit-testing ×1
vb.net ×1
windows ×1