我有一个MVC3应用程序,它从用户的硬盘驱动器上传文件并进行操作.一个要求是文件扩展名应为.xls(或xlsx).
我想验证文件名,但我想知道什么是可重用性和性能(当然还有最佳编码实践)的最佳选择.
我有以下索引视图:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<br />
<p><input type="file" id="file" name="file" size="23"/></p><br />
<p><input type="submit" value="Upload file" /></p>
}
Run Code Online (Sandbox Code Playgroud)
由控制器动作方法索引:
public ActionResult Index()
{
return View("Index");
}
Run Code Online (Sandbox Code Playgroud)
用户响应由Index操作方法(HttpPost)处理:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
FileServices lfileServices = new FileServices();
var lfilePath = lfileServices.UploadFile(file, "~/App_Data/uploads");
//Manipulation;
}
Run Code Online (Sandbox Code Playgroud)
现在我可以使用Path.GetExtension(filename)FileServices中的内容来获取扩展,然后执行检查,但它将在上载完成后执行.
一旦用户尝试上传文件,我想这样做.是到了我心中的唯一事情是建立一个模型(或更好的视图模型),并使用远程验证从DataAnnotations.
我看到了Scott …
我想将一些参数作为数组传递给Web方法.Web方法的签名没有params关键字.
我有一个可变数量的参数(因为web方法接受)所以我不能将数组放入n个单个变量.
如何才能做到这一点?
我有两个大小相同的数组,arrayKeys和arrayValues分别填充了数据(分别用于键和值),以及一个空Dictionary<K, V>(myDictionary)。我想将每个数组中的元素分配为字典的键和值。我知道这可以通过使用以下代码来完成:
for(i=0:i<arrayKeys.Lenght;i++)
{
myDictionary.Add(arrayKeys[i], arrayValue[i]);
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有任何方法可以执行如下任务:
myDictionary.Keys = arrayKeys;
myDictionary.Values = arrayValues;
Run Code Online (Sandbox Code Playgroud)
也许可以通过将 lambda 表达式与 ToDictionary 方法一起使用。提前致谢
弗朗西斯科
我正在使用C#和Razor在ASP.NET MVC3中开发一个Web应用程序.我需要在我的网页中创建标签,就像在浏览器中显示一样.
我不知道MVC3是否已经提供了一个模板,或者创建标签是一个比我想象的更容易的任务.实际上这是我第一次被要求这样做.
有人有什么建议或建议吗?一开始我打算用回发来做这件事,以免在jQuery或其他客户端技术上浪费太多时间.非常感谢你:D
在我的数据库中,我有一个类型为varchar的字段日期,其中日期以yyyyMMddhhmm格式存储,没有空格或其他字符将它们分开.
现在我需要将此日期与C#DateTime进行比较,因此我需要将字符串转换为DateTime.我能想到的最合乎逻辑的方法是从变量中提取date与年,月和日相关的子字符串并创建一个新的DateTime对象:
var year = Convert.ToInt32(date.Substring(0, 4));
var month = Convert.ToInt32(date.Substring(4, 2));
var day = Convert.ToInt32(date.Substring(6, 2));
DateTime dateToCompare = new DateTime(year, month, day);
Run Code Online (Sandbox Code Playgroud)
是否有任何可用的C#方法允许我在不编写所有代码的情况下执行此转换?
我想创建一个包含从0到1的值的数组,间隔为0.1.我可以用:
float[] myArray = new float[10];
float increment = 0.1;
for(i = 0; i < 10; i++)
{
myArray[i] = increment;
increment += 0.1;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一个函数Enumerable.Range允许指定增量间隔.
我有以下课程:
class Product
{
public string ProductName { get; set; }
public DateTime ActivationDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我创建并填写一个 List<Product>,我想ProductName从Product最新的ActivationDate.
Product.Where(m => m.ActivationDate == Max(m.ActivationDate)).Select(n => n.ProductName)
Product.Max(m => m.ActivationDate).Select(n => n.ProductName)
Run Code Online (Sandbox Code Playgroud)
但机器人方法不起作用.有人知道实现这项任务的方法吗?
我有以下Product课程:
public class Product
{
public string Name { get; set; }
public float Price { get; set; }
public int? CategoryId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我必须计算Product每个人有多少CategoryId并将它们放入一个Dictionary<int, int>.因此:
IQueryable<Product> products = _productServices.GetAll(); //return IQueryable<Product>
Dictionary<int, int> productDict = products.ToList()
.GroupBy(p => p.CategoryId)
.ToDictionary(pgroup => pgroup.key, pgroup => pgroup.Count());
Run Code Online (Sandbox Code Playgroud)
问题是,我得到一个Dictionary<int?, int>从ToDictionary().即使我通过放置预过滤空值Where(p => p.CategoryId != null)我不改变CategoryIdto 的类型int.我还尝试创建和匿名类型:
products.ToList()
.GroupBy(p => p.CategoryId)
.Select(p => …Run Code Online (Sandbox Code Playgroud) 我正在维护由SQL Server中的其他人创建的数据库.在一个表中,我找到了一个名称在方括号之间的列.该字段的名称是desc,它作为表存储在表中[desc].其他字段存储时没有方括号.这个选择背后有什么特殊原因/惯例吗?
构建在数据库之上的应用程序是用C#或VB.NET开发的.
谢谢
我必须在同一域中的两个服务器之间创建MSMQ消息传递机制,SenderServer(MS Server 2012)和ReceiverServer(MS Server 2008 R2).
我在ReceiverServer中 创建了一个私有的事务性队列.\private$\receiver,我向系统和管理员提供了接收(和查看)消息权限.
然后,我创建了一个客户端应用程序,使用以下代码创建消息并将消息转发到队列:
MessageQueue queue = new queue("FormatName:Direct=OS:ReceiverServer\private$\receiver");
Message message = new Message();
message.Body = "myMessage";
using (MessageQueueTransaction tx = new MessageQueueTransaction())
{
tx.Begin();
queue.Send(message, "myLabel", tx);
tx.Commit();
}
Run Code Online (Sandbox Code Playgroud)
在部署应用程序之前,我在我的机器(Windows 7)上测试了它,它Direct=OS:ReceiverServer\private$\receiver使用State:Connected和正确地创建了一个传出队列Connection History:Connection is ready to transfer messages.消息被正确发送到ReceiverServer并放入\private$\receiver队列中.在End2End log该的ReceiverServer为每封邮件记录两个事件:
ID CN=msmq, CN=[mymachinename], CN=Computers, DC=[domain], DC=[other]已被放入队列ReceiverServer\private$\receiver(EventId:1)然后我使用相同的代码在SenderServer中 …
c# ×8
linq ×2
arrays ×1
conventions ×1
database ×1
datetime ×1
dictionary ×1
file ×1
intervals ×1
max ×1
msmq ×1
nullable ×1
razor ×1
sql-server ×1
string ×1
tabs ×1
web-services ×1