我正在使用Html.TextBox助手来创建文本框.我想在文本框上设置属性,我理解这是使用以下重载完成的:
Html.TextBox (string name, object value, object htmlAttributes)
但是,我想维护HTML帮助程序自动使用ViewData或ViewData.Model中的值的功能,我没有看到只指定名称和htmlAttributes的方法.这可能吗?
我只是编写一个控制台实用程序,并决定使用NDesk.Options进行命令行解析.我的问题是,如何强制执行所需的命令行选项?
我在文档中看到:
具有所需值的选项(将"="附加到选项名称)或可选值(将":"附加到选项名称).
但是,当我=在选项名称末尾添加a 时,行为没有区别.理想情况下,Parse方法会抛出异常.
我还需要做些什么吗?
这是我的测试代码:
class Program
{
static void Main(string[] args)
{
bool show_help = false;
string someoption = null;
var p = new OptionSet() {
{ "someoption=", "Some String Option", v => someoption = v},
{ "h|help", "show this message and exit", v => show_help = v != null }
};
List<string> extra;
try
{
extra = p.Parse(args);
}
catch (OptionException e)
{
System.Console.Write("myconsole: ");
System.Console.WriteLine(e.Message);
System.Console.WriteLine("Try `myconsole --help' for more information."); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个ASP.NET MVC应用程序,我需要将数据导出到Excel电子表格.以前,在webforms应用程序中,我使用了一些我发现的代码将GridView呈现为与excel兼容的文件.这非常方便.我想知道在MVC中最快/最有效的方法是什么.谢谢.
我试图预编译我的ASP.NET MVC应用程序并将其部署到IIS6框(带通配符映射),但是我在渲染部分视图(用户控件)时遇到错误.在预编译之前,它在我的开发机器上正常工作.
错误是:
'/'应用程序中的服务器错误.
找不到部分视图'ListGrid'
.
搜索了以下位置:
〜/ Views/Initiative/ListGrid.aspx
~/Views/Initiative/ListGrid.ascx
~/Views/Shared/ListGrid.aspx
~/Views/Shared/ListGrid.ascx
我检查了Views\Shared的文件,它不存在,我认为这是正常的,因为它预编译.但只是为了咯咯笑,我在该文件夹中放了一个空白文件名为ListGrid.ascx,但后来我收到了这个错误:
'/'应用程序中的服务器错误.
文件'/Views/Shared/ListGrid.ascx'
尚未预编译,无法
请求.
我用Google搜索并搜索了但是找不到任何类似的问题,但没有运气.
我正在C#4中编写一个Console应用程序,想要优雅地取消我的程序并按下Ctrl + C. 我之前使用过很多次代码,但是现在尝试在.NET 4中使用它时,似乎发生了一个奇怪的未处理异常.
namespace ConsoleTest
{
class Program
{
private static bool stop = false;
static void Main(string[] args)
{
System.Console.TreatControlCAsInput = false;
System.Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
while (!stop)
{
System.Console.WriteLine("waiting...");
System.Threading.Thread.Sleep(1000);
}
System.Console.WriteLine("Press any key to exit...");
System.Console.ReadKey(true);
}
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
stop = true;
e.Cancel = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将目标框架更改为.NET 3.5,它可以工作.
编辑:看来这个人看到了同样的问题:http: //johnwheatley.wordpress.com/2010/04/14/net-4-control-c-event-handler-broken/
我知道这可能是使用Streams,但我不确定正确的语法.
我想将一个字符串传递给Save方法并让它gzip字符串并将其上传到Amazon S3,而不必写入磁盘.当前方法在两者之间低效地读取/写入磁盘.
S3 PutObjectRequest有一个带有InputStream输入的构造函数作为选项.
import java.io.*;
import java.util.zip.GZIPOutputStream;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class FileStore {
public static void Save(String data) throws IOException
{
File file = File.createTempFile("filemaster-", ".htm");
file.deleteOnExit();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write(data);
writer.flush();
writer.close();
String zippedFilename = gzipFile(file.getAbsolutePath());
File zippedFile = new File(zippedFilename);
zippedFile.deleteOnExit();
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
new FileInputStream("AwsCredentials.properties")));
String bucketName = "mybucket";
String key = "test/" + zippedFile.getName();
s3.putObject(new PutObjectRequest(bucketName, key, zippedFile));
}
public static String …Run Code Online (Sandbox Code Playgroud) 我目前使用以下代码从Amazon C#中检索和解压缩字符串数据:
GetObjectRequest getObjectRequest = new GetObjectRequest().WithBucketName(bucketName).WithKey(key);
using (S3Response getObjectResponse = client.GetObject(getObjectRequest))
{
using (Stream s = getObjectResponse.ResponseStream)
{
using (GZipStream gzipStream = new GZipStream(s, CompressionMode.Decompress))
{
StreamReader Reader = new StreamReader(gzipStream, Encoding.Default);
string Html = Reader.ReadToEnd();
parseFile(Html);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想反转这段代码,以便我可以压缩并将字符串数据上传到S3,而无需写入磁盘.我尝试了以下,但我得到一个例外:
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKeyID, AWSSecretAccessKeyID))
{
string awsPath = AWSS3PrefixPath + "/" + keyName+ ".htm.gz";
byte[] buffer = Encoding.UTF8.GetBytes(content);
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress))
{
zip.Write(buffer, 0, buffer.Length); …Run Code Online (Sandbox Code Playgroud) 我正在使用C#驱动程序2.0.我有一个POCO,我存储在mongo中,看起来像这样:
public class TestObject
{
[BsonId]
public Guid Id { get; set; }
public string Property1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我使用这样的通用方法存储对象:
public async void Insert<T>(T item)
{
var collection = GetCollection<T>();
await collection.InsertOneAsync(item);
}
Run Code Online (Sandbox Code Playgroud)
我想有一个类似的方法来更新对象.但是,该ReplaceOneAsync方法需要指定过滤器.
我想简单地根据[BsonId]属性来更新同一个对象.任何人都知道这是否可能?
我正在配置一个新环境来运行多个Intranet Web应用程序.我有2台服务器,一台是SQL Server 2008服务器,另一台是IIS服务器.我还需要安装SQL Server Reporting Services.我不确定在数据库服务器或Web服务器上运行报告服务是否更好.这种情况有最好的做法吗?
我手动将ASP.NET MVC添加到现有的WebForms应用程序......感谢 本教程.
但是,现在我没有菜单选项(右键单击Controllers文件夹或Views文件夹)以显示Add Controller或Add View对话框.在创建一个全新的MVC项目时,我确实有这个.我怎样才能让visual studio意识到我在这个混合项目中使用MVC?
我正在使用MVCGrid.net(http://mvcgrid.net).首先,神奇的网格工具!我的网格正常工作,但是当我填充网格时,我需要将另一个参数传递给我的RetrieveDataMethod.我需要传递的值是我的视图模型的一部分,我很确定我需要将它作为AdditionalQueryOption传递.我不希望这是一个过滤器,因为我不希望它只在客户端事件后发送.我希望它总是传递给我的RetrieveDataMethod.我尝试将data-mvcgrid-type ='additionalQueryOption'添加到我的隐藏输入中,但仍未发送.然后我注意到这需要data-mvcgrid-apply-additional ='event'来触发事件.这与过滤器有什么不同?是否有一个网格加载事件,我可以挂钩注册我的隐藏值作为一个额外的查询选项?对我的情况有什么正确的解决方法?
这是我的网格的检索数据方法定义:
.WithRetrieveDataMethod((context) =>
{
var options = context.QueryOptions;
// this is the bit i need
var projectId = ???;
options.AdditionalQueryOptions.Add("projectId", projectId);
int totalRecords;
var items = ReportManager.GetReports(out totalRecords, options);
return new QueryResult<ReportSummaryViewModel>()
{
Items = items,
TotalRecords = totalRecords
};
})
Run Code Online (Sandbox Code Playgroud)
这是查看代码:
<h1>Reports for @Model.ProjectName</h1>
<p class="form-inline">
<button type="button" class="btn btn-default" onclick="window.location.href='@Url.Action("Index", "Home")'">Back</button>
<button type="button" class="btn btn-primary" onclick="window.location.href='@Url.Action("Create", "Reports", new { @id = Model.ProjectId })'">New Report</button>
</p>
<!-- This is the hidden input i'd like …Run Code Online (Sandbox Code Playgroud) 我在以前的mongodb c#驱动程序中找到了很多如何使用$ in的示例,但是在2.0版本中找不到任何如何使用$ in的示例。
我正在使用MVC3并且在代码中的某些位置我正在使用该System.ComponentModel.DataAnnotations.DataType.EmailAddress属性并让MVC模型验证为我进行验证.
但是,我现在想要在不使用模型的代码的不同部分验证电子邮件地址.我想使用已经被MVC使用的相同方法,但是我无法找到有关如何执行此操作的任何信息.
编辑 - 对不起,如果我的问题不清楚.我会试着澄清一下.
这是RegisterModel的一个片段,它包含在默认的MVC模板中:
public class RegisterModel
{
...
[Required]
[DataType(DataType.EmailAddress)]
[DisplayName("Email address")]
public string Email { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
这些属性指示mvcs模型验证如何验证此模型.
但是,我有一个应该包含电子邮件地址的字符串.我想以与 mvc 相同的方式验证电子邮件地址.
string email = "noone@nowhere.com";
bool isValid = SomeMethodForValidatingTheEmailAddressTheSameWayMVCDoes(email);
Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×5
c# ×4
amazon-s3 ×2
.net-4.0 ×1
console ×1
java ×1
mongodb ×1
mvcgrid ×1
mvcgrid.net ×1
sql-server ×1