我对下面的代码有点困惑。它在 Windows 7 上的 VS2010 中运行良好,现在我已经将硬件升级到 Windows 8 和 VS 2012,但它没有。
我的 MVC 应用程序中有以下 JavaScript 代码:
<script language="javascript" type="text/javascript">
var today;
if("@Model.Birthday.HasValue"){
var today = new Date("@Model.Birthday.Value.Year", "@Model.Birthday.Value.Month" - 1, "@Model.Birthday.Value.Day");
}else{
today = new Date();
}
模型从具有如下属性的 ViewModel 拉取:
public System.DateTime? Birthday
{
get { return user.Birthday; }
set { user.Birthday = value; }
}
异常抛出行:
var today = new Date("@Model.Birthday.Value.Year", "@Model.Birthday.Value.Month" - 1, "@Model.Birthday.Value.Day");
它返回“Nullable object must have value”,如果它跳转到该行执行,它应该有值。但是,我的 if 语句返回 false。如果我删除该行并只放入一个警报语句,它永远不会执行该行或显示警报,因为 Birthday 属性为空。 JS 执行 MVC 代码的方式似乎发生了一些变化,它似乎会评估整个代码块,而不考虑 if 语句。我也尝试了以下方法,但无济于事:
这应该很简单 - 测试一个值是否为 null,如果不是,则从 .NET 属性创建一个 JS Date 对象,如果是,则创建一个新的 JS Date 对象,但由于某种原因,它想要评估并执行该行,即使 HasValue 为 false。
真的被难住了。有没有人见过这样的事情?
最佳答案
我可以看到这里发生的一些事情正在影响您呈现正确数据的能力。
首先,MVC 变量在服务器端进行处理。您当前的实现总是尝试在 Javascript 中为 @Model.Birthday.Value 属性呈现一个值,以便生成 HTML。无论当前示例中的属性是真还是假,都会发生这种情况,因为它依赖于客户端 Javascript 来执行条件而不是 Razor。以下是您的问题的完整工作示例。
查看模型
namespace MvcApplication1.Models
{
public class TestViewModel
{
public DateTime? NullDate { get; set; }
public DateTime? DateWithValue { get; set; }
}
}
Controller
public ActionResult Index()
{
return View(new TestViewModel{DateWithValue = DateTime.Now,
NullDate = null});
}
查看
@model MvcApplication1.Models.TestViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script type="text/javascript">
var today;
var anotherDay;
@{if(Model.NullDate.HasValue)
{
@: alert('Null date had a value!');
@: today = new Date('@Model.NullDate.Value.Year', parseInt('@Model.DateWithValue.Value.Month') - 1, '@Model.NullDate.Value.Day');
}else
{
@: alert('Null date did not have a value');
@: today = new Date();
}}
@{if (Model.DateWithValue.HasValue)
{
@: alert('DateWithValue had a value!');
@: anotherDay = new Date('@Model.DateWithValue.Value.Year', parseInt('@Model.DateWithValue.Value.Month') - 1, '@Model.DateWithValue.Value.Day');
@: alert(anotherDay);
}
else
{
@: alert('DateWithValue date did not have a value');
@: anotherDay = new Date();
}}
</script>
很明显,代码在 Javascript 部分是重复的,以显示它适用于同一 Controller 操作中的 Null 和 Non-null 值。
如您在 View 中所见,我们确保使用 Razor 语法检查模型,并使用 Javascript 设置实际的客户端变量。另外,我想知道您的“-1”是否正常工作。为了在这个例子中做减法,我不得不从 Javascript 将该值转换为 Int,但这是一个次要的细节。
作为引用,这是一个在 VS2012 上的 MVC 4 项目,使用 Chrome 测试了 Windows 8 Professional。
关于Javascript 在 If 语句中错误地执行 MVC 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13218759/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee