查看下面的代码并告诉我为什么在调用函数UpdateContext之后,变量connection2没有与变量connection1相同的哈希码.
当我将变量connection2设置为connection1时,两个变量都有一个指向相同内存地址的指针.但是在函数UpdateContext中通过ref传递变量connection1后,使用'new'指令修改指针,connection1有一个新的指针地址,但connection2仍然是旧地址.
class Program
{
static void Main(string[] args)
{
var connectionInitializer = new ConnectionInitializer();
connectionInitializer.Initialize();
Console.ReadLine();
}
}
public class Connection
{
}
public class ConnectionInitializer
{
public void Initialize()
{
var connection1 = new Connection();
var connection2 = connection1;
Console.WriteLine("Connection 1 (Before ref): " + connection1.GetHashCode());
Console.WriteLine("Connection 2 (Before ref): " + connection2.GetHashCode());
this.UpdateContext(ref connection1);
Console.WriteLine("Connection 1 (After ref): " + connection1.GetHashCode());
Console.WriteLine("Connection 2 (After ref): " + connection2.GetHashCode());
}
private void UpdateContext(ref Connection connection)
{
connection = …
Run Code Online (Sandbox Code Playgroud) 我尝试用Telerik的控件创建一个html帮助器,但为了做到这一点,我必须在文件的顶部有这个指令:
@using Telerik.Web.Mvc.UI
问题出在html helper中,当我输入'@using'然后尝试键入'Telerik'时,visual studio告诉我,即使我已经在同一项目中的许多其他地方使用了该指令,该命名空间也不存在.
正如您所看到的那样,Telerik命名空间会产生错误; '无法解决符号......
这是同一项目的某个EditorTemplates文件夹中的相同代码,但在这种情况下,没有错误:
是因为除了MVC框架之外我不可能拥有控制权或者我错过了什么?
谢谢.
编辑:
在Darin的帮助下搜索后,我终于找到了问题所在.您可以查看Darin的评论答案.
另外,这是我的最终代码:
@using System.Web.Mvc
@using Telerik.Web.Mvc.UI
@helper IntegerTextBox(System.Web.Mvc.HtmlHelper html, string id, int? value, bool enable = true)
{
@(html.Telerik().IntegerTextBox()
.Name(id)
.InputHtmlAttributes(new {style = "width:200px"})
.Spinners(false)
.EmptyMessage("")
.MaxValue(int.MinValue)
.MaxValue(int.MaxValue)
.Value(value)
.Enable(enable)
)
}
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,你不能使用@using指令到System.Web.Mvc来减少第一个参数的完全限定类型.我不知道为什么......'HtmlHelper'看起来像'html helper'或'@helper'......这个词与MVC框架中的某些内容有冲突?我不知道!?:(
我最近使用了由Ryan Niemeyer创建的knockoutjs和kendo UI的精彩绑定,我尝试找出javascript代码中的一些功能.
当我们没有传递这样的参数时,JQuery究竟做了什么:
if (!$()["kendoComboBox"]) {
...
}
Run Code Online (Sandbox Code Playgroud) 我在视图中有一个表单,我使用Ajax.BeginForm.我想用javascript代码更改提交按钮的默认行为.我用.on()方法附加了一个处理程序,在处理程序中,我调用了preventDefault()方法.我想知道为什么当我将处理程序附加到根DOM元素时,默认行为不会被取消,但是当我将处理程序直接附加到表单DOM元素时,默认行为将被取消.
风景:
@using (this.Ajax.BeginForm("GetList", "Report", null, new AjaxOptions(), new {id = "OptionForm"}))
{
<input type="submit" id="submitButton"/>
}
Run Code Online (Sandbox Code Playgroud)
将处理程序附加到根DOM元素的脚本(不起作用):
<script type="text/javascript">
$(function () {
$(document).on("submit", "#OptionForm", refreshGrid);
})
</script>
Run Code Online (Sandbox Code Playgroud)
处理程序:
refreshGrid: function (event) {
event.preventDefault();
$.ajax({
url: url,
type: "POST",
data: $(this).serialize(),
success: function (data) {
if (data.success) {
$("#ReferenceList").html(data.result);
}
else {
$("#ValidationSummary").html(data.result);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
最后将处理程序附加到表单DOM元素(Works)的代码:
<script type="text/javascript">
$(function () {
$("#OptionForm").submit = refreshGrid;
})
</script>
Run Code Online (Sandbox Code Playgroud)
谢谢.