如何在mvc视图中编写注释,不会传输到最终的html(即浏览器,响应).人们可以发表评论
<!--<a href="/">My comment</a> -->
Run Code Online (Sandbox Code Playgroud)
但它在浏览器的页面源代码中可见.
是否可以在cshtml文件中留下评论仅供内部使用?
在某些情况下,当字典中没有这样的键时,对于我来说,有一个简短的,可读的方式来获取null而不是KeyNotFoundException按键访问字典值,这似乎是有用的.
我想到的第一件事是扩展方法:
public static U GetValueByKeyOrNull<T, U>(this Dictionary<T, U> dict, T key)
where U : class //it's acceptable for me to have this constraint
{
if (dict.ContainsKey(key))
return dict[key];
else
//it could be default(U) to use without U class constraint
//however, I didn't need this.
return null;
}
Run Code Online (Sandbox Code Playgroud)
但是当你写下这样的东西时,它实际上并不是很短暂的说法:
string.Format("{0}:{1};{2}:{3}",
dict.GetValueByKeyOrNull("key1"),
dict.GetValueByKeyOrNull("key2"),
dict.GetValueByKeyOrNull("key3"),
dict.GetValueByKeyOrNull("key4"));
Run Code Online (Sandbox Code Playgroud)
我会说,有一些接近基本语法的东西要好得多:dict["key4"].
然后我想出了一个带有private字典字段的类的想法,它暴露了我需要的功能:
public class MyDictionary<T, U> //here I may add any of interfaces, implemented
//by dictionary itself to …Run Code Online (Sandbox Code Playgroud) 我.on()在jQuery 1.7中使用过,并想知道是否可以为已经注入页面的元素一次附加多个选择器.以前,我正在使用live()- 但很明显,为什么我想要改进性能.
你能.on()以这样的方式使用:
$(document).on('click', '#selector1, #selector2, .class1', function () {
//stuff
});
Run Code Online (Sandbox Code Playgroud)
附加到文档中是否有任何好处?
?
我试着按照如何在C#教程中打开Excel文件,即在Com选项卡上添加了一个引用Microsoft Office 14.0 Object Library并尝试编译代码:
using Excel = Microsoft.Office.Interop.Excel;
//...
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlApp = new Excel.ApplicationClass();//error here
//...
Run Code Online (Sandbox Code Playgroud)
并且面临编译错误,说
没有为Microsoft.Office.Interop.Excel.ApplicationClass类型定义构造函数.
我错过了什么?
如何将Datetimepicker-Format 设置为MM/YYYY?
谷歌搜索了一段时间后,我对如何在asp mvc 3中这样做有点困惑.
因此,任务是为几个控制器的视图提供一个公共布局(或主?)页面.所有视图都是强类型的...这个布局页面实际上可视化一些强类型的对象.所以,我需要将这个对象传递给布局页面,将它传递给`Html.RenderPartial()'或者在页面中正确呈现它.
文章将数据传递给查看母版页(C#)(虽然是mvc2的旧版本)提供了一个示例来提供基本抽象控制器类,并将模型传递给ViewData.这可能是治愈方法.
但...
在某些线程中,我找到了一种可以创建强类型母版页的方法:
<%@ Master ... Inherits="System.Web.Mvc.ViewMasterPage<MyWeb.Models.Client>" %>
Run Code Online (Sandbox Code Playgroud)
那么,mvc3中有相同的方法吗?或者将数据传递到布局页面的唯一方法是使用ViewBag还是ViewData?
在阅读了什么是在asp.net-mvc中将ViewModel数据绑定到母版页的最佳方法之后,我已经考虑了Darin的答案,但仍然认为这一组视图将具有相同的布局,并且没有理由反对DRY原理.
那么,如何将强类型对象传递给布局页面?或者我混淆了什么?
请考虑以下路线:
routes.MapRoute(
"route1",
"{controller}/{month}-{year}/{action}/{user}"
);
routes.MapRoute(
"route2",
"{controller}/{month}-{year}/{action}"
);
Run Code Online (Sandbox Code Playgroud)
以下测试:
测试1
[TestMethod]
public void Test1()
{
RouteCollection routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
RequestContext context = new RequestContext(CreateHttpContext(),
new RouteData());
DateTime now = DateTime.Now;
string result;
context.RouteData.Values.Add("controller", "Home");
context.RouteData.Values.Add("action", "Index");
context.RouteData.Values.Add("user", "user1");
result = UrlHelper.GenerateUrl(null, "Index", null,
new RouteValueDictionary(
new
{
month = now.Month,
year = now.Year
}),
routes, context, true);
//OK, result == /Home/10-2012/Index/user1
Assert.AreEqual(string.Format("/Home/{0}-{1}/Index/user1", now.Month, now.Year),
result);
}
Run Code Online (Sandbox Code Playgroud)
测试2
[TestMethod]
public void Test2()
{
RouteCollection routes = new RouteCollection(); …Run Code Online (Sandbox Code Playgroud) 说,我们有2个班:
public class A
{
public int a;
}
public class B
{
public int b;
public static implicit operator B(A x)
{
return new B { b = x.a };
}
}
Run Code Online (Sandbox Code Playgroud)
那么为什么
A a = new A { a = 0 };
B b = a; //OK
List<A> listA = new List<A> { new A { a = 0 } };
List<B> listB = listA.Cast<B>().ToList(); //throws InvalidCastException
Run Code Online (Sandbox Code Playgroud)
同样的explicit操作.
PS:手动(separetely)投射每个元素
List<B> listB = listA.Select<A, B>(s => s).ToList(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试确定是否启用了AD中的用户帐户.为此,我使用以下代码:
string domain = "my domain";
string group = "my security group";
string ou = "my OU";
//init context
using (var cnt= new PrincipalContext(ContextType.Domain, domain))
{
//find the necessary security group
using (GroupPrincipal mainGroup
= GroupPrincipal.FindByIdentity(cnt, IdentityType.Guid, group))
{
if (mainGroup != null)
{
//get the group's members
foreach (var user in mainGroup.GetMembers()
.OfType<UserPrincipal>()
.Where(u => u.DistinguishedName.Contains(ou)))
{
//ensure that all the info about the account is loaded
//by using FindByIdentity as opposed to GetMembers
var tmpUser= UserPrincipal.FindByIdentity(cnt,
user.SamAccountName);
//actually …Run Code Online (Sandbox Code Playgroud) 我有一个DisplayedData类......
public class DisplayedData
{
private int _key;
private String _username;
private String _fullName;
private string _activated;
private string _suspended;
public int key { get { return _key; } set { _key = value; } }
public string username { get { return _username; } set { _username = value; } }
public string fullname { get { return _fullName; } set { _fullName = value; } }
public string activated { get { return _activated; } set …Run Code Online (Sandbox Code Playgroud) c# ×9
arrays ×1
casting ×1
class ×1
comments ×1
delegates ×1
dictionary ×1
excel ×1
ienumerable ×1
interop ×1
jquery ×1
live ×1
master-pages ×1
razor ×1