在ASP.NET MVC2中我使用OutputCache和VaryByParam属性.我用一个参数就可以正常工作,但是当我在方法上有几个参数时,正确的语法是什么?
[OutputCache(Duration=30, VaryByParam = "customerId"]
public ActionResult Index(int customerId)
{
//I've got this one under control, since it only has one parameter
}
[OutputCache(Duration=30, VaryByParam = "customerId"]
public ActionResult Index(int customerId, int languageId)
{
//What is the correct syntax for VaryByParam now that I have a second parameter?
}
Run Code Online (Sandbox Code Playgroud)
如何使用这两个参数来缓存页面?我输入两次添加属性吗?或者写"customerId,languageId"作为值?
假设我有一个简单的模型来解释目的:
public class Category
{
...
public IEnumerable<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图:
@model Category
...
<ul>
@Html.EditorFor(m => m.Products)
</ul>
Run Code Online (Sandbox Code Playgroud)
EditorTemplate:
@model Product
...
<li>
@Html.EditorFor(m => m.Name)
</li>
Run Code Online (Sandbox Code Playgroud)
请注意,我不必定义EditorTemplate IEnumerable<Product>,我只能为Product模型创建它,而MVC框架足够聪明,可以使用自己的IEnumerable模板.它遍历我的集合并调用我的EditorTemplate.
输出html将是这样的
...
<li>
<input id="Products_i_Name" name="Products[i].Name" type="text" value="SomeName">
</li>
Run Code Online (Sandbox Code Playgroud)
毕竟,我可以发布到我的控制器.
但是,当我使用模板名称定义EditorTemplate时,为什么MVC没有做到这一点?
@Html.EditorFor(m => m.Products, "ProductTemplate")
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我必须更改属性的类型,IList<Product>自己遍历集合并调用EditorTemplate
@for (int i = 0; i < Model.Products.Count; i++)
{
@Html.EditorFor(m => m.Products[i], "ProductTemplate")
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一种肮脏的解决方法给我.这样做是否还有其他更清洁的解决方案?
我尝试将JSON对象发送回服务器.这是我的AJAX电话:
$.ajax({
url: '/Home/NewService',
async: false,
type: "POST",
data: JSON.stringify(props),
error: function (jqXHR, textStatus, errorThrown) {
console.log("FAIL: " + errorThrown);
},
success: function (data, textStatus, jqXHR) {
console.log("SUCCES");
}
});
Run Code Online (Sandbox Code Playgroud)
JSON.stringify(props)浏览器调试器的评估是
"[{"name":"firstName","value":"firstValue"}]"
这是被调用的控制器中的方法:
[HttpPost]
public void NewService(dynamic json)
{
Response.Write(json);
}
Run Code Online (Sandbox Code Playgroud)
我json遇到的问题是,上面的变量总是一个空对象.该success函数被调用,但是当我调试jsonVAR显示为空.
请告诉我我做错了什么.谢谢.
我正在尝试在 .Net core 上配置 Serilog 以使用命名连接字符串写入 SQL 表。如果我将连接字符串直接放在 appsettings 文件的“Serilog”部分中,我就可以让一切正常工作。但我希望它使用与实体框架相同的连接字符串,所以我不想定义它两次。另外,出于安全原因,建议似乎不要将此信息直接存储在 appsettings 文件中。
这是设置文件的相关部分:
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Development",
"schemaName": "app",
"tableName": "Logs",
"restrictedToMinimumLevel": "Information"
}
}
Run Code Online (Sandbox Code Playgroud)
开发连接字符串是在 Visual Studio 中的“用户机密”中定义的。
{
"ConnectionStrings": {
"Development": "Server=...",
"Staging": "Server=...",
"Production": "Server=..."
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所提到的,如果我用实际的字符串替换“Development”,它就可以工作。我还应该补充一点,实体框架正在使用开发连接字符串,因此我非常确定该字符串本身是好的。
我以这种方式在 Startup.Configure 方法中加载配置
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(_configuration)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我在同一方法中手动添加 MSSqlServer 对象,它将起作用(在删除 appsetting 文件的 MSSqlServer 部分之后)。显然这并不理想,因为更改任何属性都需要重新编译应用程序
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(_configuration)
.WriteTo.MSSqlServer(
connectionString: _configuration.GetConnectionString(env.EnvironmentName),
schemaName: "app",
tableName: "Logs",
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information
)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
我在 GitHub 上找到了这个线程 …
我正在尝试将我的视图模型数据转换为JSON,但是,我似乎无法访问@Json.Encode.我添加了System.Web.Helpers引用,但是没有用.我究竟做错了什么?
@Html.Password("pasword", null, new { @class = "form-control frmField", placeholder = "Password" })
Run Code Online (Sandbox Code Playgroud)
我正在使用这个,我希望这里应该有一个按钮,我单击它,密码就会变得可见。我知道这将通过 jquery 或 Javascript 实现,但我无法在 mvc 中理解这一点。如何在 mvc 中应用该技术?
<input data-toggle="password" type="password" data-placement="after" data-eye-class="glyphicon" data-eye-open-class="glyphicon glyphicon-eye-open" data-eye-close-class="glyphicon glyphicon-eye-close" data-eye-class-position="true" class="form-control pwd">
<input type="text" class="form-control" placeholder="password" style="display:none;" />
Run Code Online (Sandbox Code Playgroud)
我用过这个并且效果很好。在mvc中如何实现呢?
我正试图在我的.NET应用程序中获取莫斯科当地时间.
这是我走了多远:
private static Dictionary<TimeZoneEnum, string> timeZoneDict = new Dictionary<TimeZoneEnum, string>()
{
{TimeZoneEnum.RussiaStandardTime, "Russian Standard Time"}
};
public static DateTime GetDateTimeNowForTimeZone(TimeZoneEnum timezone)
{
DateTime result = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(timeZoneDict[timezone]));
return result;
}
Run Code Online (Sandbox Code Playgroud)
方法GetDateTimeNowForTimeZone给我GMT + 3时间但现在在俄罗斯有GMT + 4时间.有没有办法在.net框架中为此问题获得可靠的解决方案?
我正在尝试将Bootstrap类添加到一组单选按钮.
@Html.RadioButtonFor(model => model.Male, !Model.Male) Female
@Html.RadioButtonFor(model => model.Male, Model.Male) Male
Run Code Online (Sandbox Code Playgroud)
只是似乎没有找到正确的方法.任何帮助赞赏.谢谢
我有这样的Major模型:
public int Id { get; set; }
public string MajorName{ get; set; }
public string Password { get; set; }
public System.DateTime RegisterDate { get; set; }
public string StudentId { get; set; }
public string State { get; set; }
public int FacultyId { get; set; }
public int MajorId { get; set; }
public HttpPostedFileBase ImgFile { get; set; }
Run Code Online (Sandbox Code Playgroud)
所以我在上面的模型库中有这样的方法:
public Major FindMajorById(int id)
{
return _dbcontext.Majors.Find(id);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我通过了id,我需要专业名称:
<td>
@{
EducationRepositor.MajorRepository objmajor=new MajorRepository(); …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×7
c# ×4
javascript ×2
jquery ×2
json ×2
.net ×1
.net-core ×1
ajax ×1
lambda ×1
outputcache ×1
razor ×1
razor-2 ×1
serilog ×1
timezone ×1