我刚刚创建了一个示例MVC3应用程序来学习验证.它正在使用DataAnnotations.我创建了一个名为CustomStartLetterMatch 的自定义ValidationAttribute.它正在实现"System.Web.Mvc.IClientValidatable".我有相应的客户端代码用不引人注目的jQuery编写.这是按预期工作的.
关于自定义验证器:它比较第一个名称输入和姓氏输入.如果它们的第一个字符不相同则抛出错误.
正如我所说,该应用程序工作正常.但是当我看着rule.ValidationType = "greaterdate";我感到困惑时.我想将其更改为"anotherDefaultType"之类的其他内容.当我更改它时,它失败并出现jQuery错误.
码:
using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
namespace MyValidationTEST
{
public class Person
{
[Required(ErrorMessage = "First name required")]
public string FirstName { get; set; }
[CustomStartLetterMatch("FirstName")]
[StringLength(5,ErrorMessage = "Must be under 5 characters")]
public string LastName { get; set; }
[Range(18,50,ErrorMessage="Must be between 18 and 50")]
public int Age { get; set; }
}
public sealed class CustomStartLetterMatch : ValidationAttribute, System.Web.Mvc.IClientValidatable
{
private const string _defaultErrorMessage …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Excel作为数据源进行单元测试.我收到以下异常.我们如何纠正它?
单元测试适配器无法连接到数据源或读取数据.有关解决此错误的详细信息,请参阅"数据驱动的单元测试疑难解答"
[TestMethod]
[Owner("Lijo ")]
[TestProperty("TestCategory", "Developer"),
DataSource("Microsoft.ACE.OLEDB.12.0",
"Data Source=C:/Sheets/DataSheet.xlsx;Extended Properties=Excel 12.0;",
"[Sheet1$]",
DataAccessMethod.Sequential)]
public void ChangePasswordTest()
{
int a = Convert.ToInt32(TestContext.DataRow[0]); //(int)Column.UserId
int b = Convert.ToInt32(TestContext.DataRow[1]);
int expectedResult = Convert.ToInt32(TestContext.DataRow[2]);
MyClass myObj = new MyClass(1, "P@ssw0rd");
int actualResult = myObj.GetAdditionResult(a, b);
Assert.AreEqual<int>(expectedResult, actualResult, "The addition result is incorrect.");
}
Run Code Online (Sandbox Code Playgroud)
阅读:
我BankAccount上课了.FixedBankAccount并SavingsBankAccount从中衍生出来.
如果收到的对象不是派生对象,我需要抛出异常.我有以下代码.
IEnumerable<DBML_Project.BankAccount> accounts = AccountRepository.GetAllAccountsForUser(userId);
foreach (DBML_Project.BankAccount acc in accounts)
{
string typeResult = Convert.ToString(acc.GetType());
string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount));
if (String.Equals(typeResult, baseValue))
{
throw new Exception("Not correct derived type");
}
}
namespace DBML_Project
{
public partial class BankAccount
{
// Define the domain behaviors
public virtual void Freeze()
{
// Do nothing
}
}
public class FixedBankAccount : BankAccount
{
public override void Freeze()
{
this.Status = "FrozenFA";
}
}
public class SavingsBankAccount …Run Code Online (Sandbox Code Playgroud) 我在SQL Server中有一个存储过程
CREATE PROCEDURE ParseXML (@InputXML xml)
Run Code Online (Sandbox Code Playgroud)
输入参数的数据类型是"xml".
在LINQ to SQL生成的存储过程代码中,输入参数是System.Xml.Linq.XElement
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.ParseXML")]
public ISingleResult<ParseXMLResult> ParseXML([global::System.Data.Linq.Mapping.ParameterAttribute(Name="InputXML", DbType="Xml")] System.Xml.Linq.XElement inputXML)
Run Code Online (Sandbox Code Playgroud)
现在,如何将以下List传递给ParseXML方法以使存储过程正常工作?
编辑:
阅读答案后 - 下面列出了另一种解决方案
XElement root = new XElement("ArrayOfBankAccountDTOForStatus",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"));
foreach (var element in bankAccountDTOList)
{
XElement ex= new XElement("BankAccountDTOForStatus",
new XElement("BankAccountID", element.BankAccountID),
new XElement("Status", element.Status));
root.Add(ex);
}
Run Code Online (Sandbox Code Playgroud)
问题代码
string connectionstring = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";
var theDataContext = new DBML_Project.MyDataClassesDataContext(connectionstring);
List<DTOLayer.BankAccountDTOForStatus> bankAccountDTOList = new List<DTOLayer.BankAccountDTOForStatus>();
DTOLayer.BankAccountDTOForStatus presentAccount1 = new …Run Code Online (Sandbox Code Playgroud) 我有一个Report对象,它具有Recipients属性(String数据类型)."收件人"属性将保留所有收件人的电子邮件地址comma separated string.我需要从逗号分隔的字符串创建电子邮件对象的" 集合 ".我有以下代码使用字符串列表 来获取电子邮件地址.然后我创建了一个电子邮件对象的集合.
有没有更好的方法来避免冗余的List和Collection使用LINQ?
Report report = new Report();
report.Recipients = "test@test.com, demo@demo.com";
List<string> emailAddressList = new List<string>( report.Recipients.Split(',') );
Collection<Email> emailObjectCollection = new Collection<Email>();
foreach (string emailAddress in emailAddressList)
{
Email email = new Email();
email.EmailAddress = emailAddress;
emailObjectCollection.Add(email);
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
CA1002:不要公开通用列表.System.Collections.Generic.List是一个通用集合,专为性能而非继承而设计,因此不包含任何虚拟成员.http://msdn.microsoft.com/en-us/library/ms182142(v=vs.80).aspx
我有以下字符串:
string source = "Test/Company/Business/Department/Logs.tvs/v1";
Run Code Online (Sandbox Code Playgroud)
该/字符是字符串中各种元素之间的分隔符.我需要获取字符串的最后两个元素.我为此目的有以下代码.这很好用.有没有更快/更简单的代码?
码
static void Main()
{
string component = String.Empty;
string version = String.Empty;
string source = "Test/Company/Business/Department/Logs.tvs/v1";
if (!String.IsNullOrEmpty(source))
{
String[] partsOfSource = source.Split('/');
if (partsOfSource != null)
{
if (partsOfSource.Length > 2)
{
component = partsOfSource[partsOfSource.Length - 2];
}
if (partsOfSource.Length > 1)
{
version = partsOfSource[partsOfSource.Length - 1];
}
}
}
Console.WriteLine(component);
Console.WriteLine(version);
Console.Read();
}
Run Code Online (Sandbox Code Playgroud) 我们有一个如下所列的类结构
public class ItemDTO
{
public int ItemID { get; set; }
}
public class CostPageDTO
{
public string CostPageNumber { get; set; }
public List<ItemDTO> Items { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有两个列表 - 1)数据库中存在的Costpage和Items列表2)用户选择的costpages
题
我们需要比较这两个列表,并得到一个结果列表,其中包含成本页面,实际集合和选定集合中不同项目的计数相同.
什么是best performingLINQ(链式方法)?
预期结果
基于以下场景的预期结果是仅包含1个成本页的列表 - "C2"(项目匹配)

码
static void Main(string[] args)
{
List<CostPageDTO> selectedCostPageAndItems = GetSelectedCostPageAndItems();
List<CostPageDTO> actualItems = GetActualItems();
//LINQ code to get the matching count costPages
}
private static List<CostPageDTO> GetSelectedCostPageAndItems()
{
ItemDTO i1 = new ItemDTO();
i1.ItemID = …Run Code Online (Sandbox Code Playgroud) 我有两个实体 - PopularTutorial和Blog.此数据需要显示在主页视图中,如下所示.关键点是"PopularTutorial"应该在其他视图中重用,Bloglist也可以在其他视图中重用."PopularTutorial"部分中有一个手动分页选项.单击第1页时,将列出前3个热门教程.单击第2页时,将列出教程4到6.
我知道"局部视野"是要走的路.当我搜索时,我遇到了涉及jQuery和JSON的方法.我想知道这是否可以在没有明确使用jQuery和JSON的情况下完成(在RAZOR中).
你可以帮我在RAOZR帮忙吗?
说实话 - 我是在学习MVC中的AJAX之前做的一步.所以我的下一次尝试将是ajaxify它.如果你能提供一个以ajax方式工作的答案,那将是很棒的.

public class PopularTutorial
{
public int ID { get; set; }
public int NumberOfReads { get; set; }
public string Title { get; set; }
}
public class Blog
{
public int ID { get; set; }
public string Head { get; set; }
public string PostBy { get; set; }
public string Content { get; set; }
}
namespace MyArticleSummaryTEST.Controllers
{
public class HomePageViewModel
{
public IEnumerable<Blog> BlogList { get; set; …Run Code Online (Sandbox Code Playgroud) 我已使用 \xe2\x80\x9c aspnet_regiis -pa " NetFrameworkConfigurationKey " "USER" 将一些用户添加到RSA密钥容器的访问控制列表(ACL)中。如何列出已获得 NetFrameworkConfigurationKey 访问权限的所有用户?
\n\n可以通过aspnet_regiis命令吗?
\n我已经使用\的escape character用于LIKE运营商.我正在逃避四个角色
1 %2 [ 3 ]4_
当我将转义字符作为输入传递时,查询不返回值.我怎样才能使它工作?
数据插入
DECLARE @Text VARCHAR(MAX)
SET @Text = 'Error \\\ \\ C:\toolbox\line 180'
INSERT INTO Account (AccountNumber,AccountType,Duration,ModifiedTime)
VALUES (198,@Text,1,GETDATE())
Run Code Online (Sandbox Code Playgroud)
码
static void Main(string[] args)
{
string searchValue1 = @"Error \\\ \\ C:\toolbox\line 180";
string searchValue2 = @"55555";
string result1 = DisplayTest(searchValue1);
string result2 = DisplayTest(searchValue2);
Console.WriteLine("result1:: " + result1);
Console.WriteLine("result2:: " + result2);
Console.ReadLine();
}}
private static string DisplayTest(string searchValue)
{
searchValue = CustomFormat(searchValue); …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×5
linq ×4
asp.net ×3
asp.net-mvc ×2
sql-server ×2
ado.net ×1
ajax ×1
encryption ×1
jquery ×1
linq-to-sql ×1
oop ×1
razor ×1
regex ×1
sql ×1
unit-testing ×1
windows ×1
xml ×1