我怎样才能创建一个类似的jQuery函数
$.MyFunction(/*optional parameter*/)?
Run Code Online (Sandbox Code Playgroud)
哪个会回归bool?
注意:
我试过这个:
jQuery.fn.isValidRequest = function (options) {
return true;
}
// I'm contending with Prototype, so I have to use this
jQuery(document).ready(function ($) {
// and jQuery 1.2.6, supplied by the client - long story
$('a').livequery('click', function () {
alert($.isValidRequest("blah"));
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
但它在alert()上崩溃了
Microsoft JScript runtime error: Object doesn't support this property or method
Run Code Online (Sandbox Code Playgroud)
这最终有效:
jQuery.isValidRequest = function (options) {
return true;
}
Run Code Online (Sandbox Code Playgroud) 在某些情况下,我将jQuery.get()请求发送到Action方法,在其他情况下,它是一个浏览器请求.有没有办法让我区分它们,以便我可以相应地返回不同的动作结果?
我有以下方法:
var catIds = DetachedCriteria.For<Category>()
.Add<Category>(c => c.TypeCode == "IMA")
.SetProjection(LambdaProjection.Property<Category>(s => s.Id));
Run Code Online (Sandbox Code Playgroud)
这没有返回任何内容,因为在数据库中该字段是nchar(10).我想要Trim()TypeCode值,如下所示:
var catIds = DetachedCriteria.For<Category>()
.Add<Category>(c => c.TypeCode.Trim() == "IMA")
.SetProjection(LambdaProjection.Property<Category>(s => s.Id));
Run Code Online (Sandbox Code Playgroud)
但它返回NHibernate错误:
Unrecognised method call in epression c.TypeCode.Trim()
Run Code Online (Sandbox Code Playgroud)
办公室里的其中一个人认为这是因为HHibernate不知道如何转换.Trim()为SQL(或者那些沿着这些方向的东西).任何人都可以建议我如何解决这个问题?
我正在尝试以编程方式向<td>C#中的元素添加第二个类.
我想要添加类的元素已经分配了一个类.
如果我做的事情
myObject.CssClass = "MyClass";
Run Code Online (Sandbox Code Playgroud)
它只是覆盖原始类.
我想我可以做点什么
myObject.CssClass += " MyClass";
Run Code Online (Sandbox Code Playgroud)
但那很丑..
*免责声明 - 我讨厌在C#中使用HTML/CSS做任何事情的想法,但我已被指派对已经恶魔般的代码库应用快速修复.在这里尝试"擦亮粪便"我没有任何意义,所以请不要让我失望!:-)
我正在尝试应用动画来折叠 LinearLayout。此动画取决于 LinearLayout 的 MeasuredHeight 属性具有大于 0 的值,但该值始终为 0。
这是我正在使用的代码:
public void Collapse()
{
var v = FindViewById<LinearLayout>(Resource.Id.layoutBranding);
int initialHeight = v.MeasuredHeight; // always 0!!
var a = new MyAnimation(v, initialHeight);
a.Duration = (int)(initialHeight / v.Context.Resources.DisplayMetrics.Density);
v.StartAnimation(a);
}
Run Code Online (Sandbox Code Playgroud)
其中 MyAnimation 定义为:
公共类 MyAnimation : 动画 { 私有只读视图 _view; 私有只读 int _initalHeight;
public MyAnimation(View view, int initalHeight)
{
_view = view;
_initalHeight = initalHeight;
}
protected override void ApplyTransformation(float interpolatedTime, Transformation t)
{
if (interpolatedTime == 1)
{
_view.Visibility = ViewStates.Gone; …Run Code Online (Sandbox Code Playgroud) 如何在没有尝试重新绘制剃刀视图的情况下将@符号添加到剃刀视图中?我问的原因是,在我的页脚中,我有一些最新的推文,它以@username格式显示推特用户名,但MVC正试图将其解析为代码并抛出编译错误,因为@username在当前上下文中不存在(明显).
任何帮助都会很棒
我使用NewtonSoft.Json将对象数据保存为JSON.
此库是否会逃避所有可能非法的字符,还是应该在尝试序列化之前转义字符?
例如
如果我有一个Site对象:
public class Site
{
public string SiteName { get; set; }
}
...
...
var site = new Site();
site.SiteName = "$$ what could I/Put 'here' %$%^*^(&*& to break this?";
var outputJson = JsonConvert.SerializeObject(site);
Run Code Online (Sandbox Code Playgroud)
我是否有可能SerializeObject()通过添加未转义的非法字符来破解该方法,或者NewtonSoft.Json是否为我这样做?
我有一个object obj传递给帮助方法.
public static MyTagGenerateTag<T>(this HtmlHelper htmlHelper, T obj /*, ... */)
{
Type t = typeof(T);
foreach (PropertyInfo prop in t.GetProperties())
{
object propValue = prop.GetValue(obj, null);
string stringValue = propValue.ToString();
dictionary.Add(prop.Name, stringValue);
}
// implement GenerateTag
}
Run Code Online (Sandbox Code Playgroud)
我被告知这不是对泛型的正确使用.有人可以告诉我,如果我没有指定泛型类型就能达到相同的结果吗?如果是这样,怎么样?
我可能会更改签名,所以它会像:
public static MyTag GenerateTag(this HtmlHelper htmlHelper, object obj /*, ... */)
{
Type t = typeof(obj);
// implement GenerateTag
}
Run Code Online (Sandbox Code Playgroud)
但是Type t = typeof(obj);不可能.
有什么建议?
谢谢
戴夫
我正在尝试重定向用户,如果他们成功登录但我的页面上的代码似乎阻止重定向工作.如果我删除重定向下面的jQuery工作.有人能告诉我,如果有什么我做错了吗?谢谢
我有以下行动:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(User user)
{
var myErrors = new Dictionary<string, string>();
try
{
if (ModelState.IsValid)
{
if (userRepository.ValidUser(user))
{
return RedirectToAction("Index", "Group", new {page = (int?)null});
}
else
{
return Json("Username or password seems to be incorrect");
}
}
else
{
foreach (KeyValuePair<string, ModelState> keyValuePair in ViewData.ModelState)
{
if (keyValuePair.Value.Errors.Count > 0)
{
List<string> errors = new List<string>();
myErrors.Add(keyValuePair.Key, keyValuePair.Value.Errors[0].ErrorMessage);
}
}
return Json(myErrors);
}
}
catch (Exception)
{
return Json("Invalid");
}
}
Run Code Online (Sandbox Code Playgroud)
以及我页面上的以下代码:
<script language="javascript" type="text/javascript"> …Run Code Online (Sandbox Code Playgroud) 我有一个基本控制器,定义如下,但是ISiteService从不执行的构造函数:
public class BaseController : Controller
{
private ISiteService _siteService;
public BaseController() {}
public BaseController(ISiteService siteService)
{
_siteService = siteService; // this never gets hit..
}
protected override void Initialize(RequestContext rc)
{
string host = ((rc.HttpContext).Request).Url.Host;
Site site = _siteService.GetSiteByHost(host); // so _siteService is null...
base.Initialize(rc);
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我为什么会这样吗?要使这个构造函数执行,我需要做什么?
实现BaseController的所有控制器都具有构造函数,这些构造函数接受StructureMap提供的各种参数,并执行所有这些构造函数.
我不知道它是否相关,但这就是我为依赖注入配置StructureMap的方法.
private void ConfigureNonOptionalDependencies()
{
// all other dependencies are registered same as this,
// and the constructors all get hit
ForRequestedType<ISiteService>()
.TheDefaultIsConcreteType<SiteService>();
}
Run Code Online (Sandbox Code Playgroud)
我对StructureMap不熟悉,所以我不知道它是否与这个问题有关,或者它是否更像是一个MVC问题.或者它甚至可能吗?谢谢
编辑:
另外,我试过这个:
public …Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net-mvc ×4
jquery ×3
controller ×2
ajax ×1
android ×1
asp.net ×1
constructor ×1
css ×1
generics ×1
json ×1
json.net ×1
nhibernate ×1
razor ×1
structuremap ×1
trim ×1
typeinfo ×1
xamarin ×1