小编JQu*_*ile的帖子

获取ASP.NET MVC 4中的Checkbox值

我正在开发一个ASP.NET MVC 4应用程序.这个程序有一个基本形式.我的表单模型如下所示:

public class MyModel
{
    public string Name { get; set; }
    public bool Remember { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在我的表单中,我有以下HTML.

<input id="Name" name="Name" type="text" value="@Model.Name" />
<input id="Remember" name="Remember" type="checkbox" value="@Model.Remember" />
<label for="Remember">&nbsp;Remember Me?</label>
Run Code Online (Sandbox Code Playgroud)

当我发布表单时,模型中的记住值始终为false.但是,模型中的Name属性具有值.我通过在以下设置断点来测试这个:

[HttpPost]
public ActionResult MyAction(MyModel model)
{
  Console.WriteLine(model.Remember.ToString());
}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚.为什么Checkbox值没有设置?

c# asp.net-mvc-4

116
推荐指数
4
解决办法
27万
查看次数

在ASP.NET MVC 4应用程序中打印版本号

我有一个ASP.NET MVC 4应用程序.目前,我正在"应用程序"选项卡下的项目属性中设置应用程序的版本.从这里,我点击"装配信息..."按钮.到那里之后,我在"Assembly version"字段中输入了"1 0 0*".

我的问题是,如何在我的网页上显示这个值?目前,我正在尝试以下方面

@System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
Run Code Online (Sandbox Code Playgroud)

不幸的是,它总是打印"0.0.0.0".实际上,我想打印1.0.0.xyz.我还想打印上次构建发生的日期/时间.但是,我不知道该怎么做.

我究竟做错了什么?

c# asp.net-mvc

64
推荐指数
4
解决办法
3万
查看次数

junit:没有发现任何测试

我继承了一个Java项目,并且是Java开发的新手.我觉得让代码感觉舒服的好方法就是围绕它编写一些测试.我正在使用IntelliJ编写代码.

我现有的项目有一个像这样的文件夹结构:

/myProject
  /src
    /main
      /java
        /com.lexcorp
          /core
            /email
              /providers
                emailProvider.java
Run Code Online (Sandbox Code Playgroud)

我创建了一个新项目,将为该项目进行测试.我希望这个项目能够同时进行单元测试和集成测试.目前,我的新项目有这样的结构:

/myProjectTests
  /src
    /main
      /java
        /com.lexcorp.core.email.providers
          emailProviderTest.java
Run Code Online (Sandbox Code Playgroud)

emailProviderTest.java文件如下所示:

package com.lexcorp.core.email.providers;

import junit.framework.TestCase;
import org.junit.Test;

public class EmailProviderTest extends TestCase {

    private final String username = "[testAccount]";

    private final String password = "[testPassword]";

    @Test
    public void thisAlwaysPasses() {
        assertTrue(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

该项目具有Run/Debug配置,具有以下属性:

  • 测试种类:全包装
  • 搜索测试:在整个项目中

当我运行此配置时,我收到一条错误消息:

junit.framework.AssertionFailedError: No tests found in com.lexcorp.core.email.providers.EmailProviderTest
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at …
Run Code Online (Sandbox Code Playgroud)

java junit intellij-idea

56
推荐指数
5
解决办法
5万
查看次数

ASP.NET MVC 4中的用户控件等效项

我正在开发一个新的ASP.NET MVC 4应用程序.我真的想以"MVC"方式做事.特别是,我对使用Razor视图引擎感兴趣.请注意,我来自网络表单背景,所以我的问题可能看起来很尴尬.

我试图了解MVC中是否存在与用户控件等效的内容?如果是这样,他们叫什么?如果没有,在MVC中创建可重用组件的推荐方法是什么?

asp.net-mvc

37
推荐指数
2
解决办法
3万
查看次数

在C#中运行时设置实体框架连接字符串

我需要在运行时设置我的Entity Framework连接字符串.现在,我有以下内容:

string connectionString = "metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework&quot;";
using (CustomerEntities entities = new CustomerEntities(connectionString))
{
  CustomerEntity entity = new CustomerEntity();
  // do more
  entities.CustomerEntities.Add(entity);
  entities.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码(替换{parameter}值)时,我收到以下错误:

不支持关键字:'数据源'.

我究竟做错了什么?

c# entity-framework

20
推荐指数
3
解决办法
4万
查看次数

使用JSON.NET解析JSON键/值对

我有一个.NET项目.我正在使用JSON.NET库.我需要使用这个库来解析一些JSON.我的JSON看起来像这样:

{"1":"Name 1","2":"Name 2"}
Run Code Online (Sandbox Code Playgroud)

该对象实际上只是键/值对的列表.我试图弄清楚如何使用JSON.NET 1)解析这个JSON和2)循环键/值对.有没有办法做到这一点?如果是这样,怎么样?

我唯一看到的是反序列化为强类型对象.

非常感谢!

c# json

18
推荐指数
2
解决办法
6万
查看次数

无法注册Bower套餐:EINVFORMAT

我正在尝试用Bower注册我的第一个包.无论我做什么,我都会收到以下错误:

bower myPackageName#*         resolve git://github.com/username/myPackageName.git#*
bower myPackageName#*        download https://github.com/username/myPackageName/archive/0.1.0.tar.gz
bower myPackageName#*         extract archive.tar.gz
bower myPackageName#*        resolved git://github.com/username/myPackageName.git#0.1.0
[?] Registering a package will make it installable via the registry (https://bower.herokuapp.com), continue? Yes
bower myPackageName         register git://github.com/username/myPackageName.git
bower                       EINVFORMAT Invalid URL format
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我会收到这个错误.有人可以向我解释我做错了什么吗?

javascript bower bower-register

13
推荐指数
1
解决办法
1641
查看次数

将图片上载到Windows Azure网站

我有一个ASP.NET MVC 4应用程序,我想部署到Windows Azure.此应用的一部分涉及上传图片.上传图片时,我想将图片存储在位于的目录中/pictures/uploaded.

我的问题是,如何将图片上传到我在Windows Azure上托管的应用程序中的相对路径?到目前为止,我的应用程序已托管在虚拟机中.我能够通过使用以下内容来完成上述操作:

string path = ConfigurationManager.AppSettings["rootWebDirectory"] + "/pictures/uploaded;

// Get the file path
if (Directory.Exists(path) == false)
  Directory.CreateDirectory(path);

string filePath = path + "/uploaded" + DateTime.UtcNow.Milliseconds + ".png";
filePath = filePath.Replace("/", "\\").Replace("\\\\", "\\");

// Write the picture to the file system
byte[] bytes = GetPictureBytes();
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
  fileStream.Write(bytes, 0, bytes.Length);
  fileStream.Flush();
  fileStream.Close();
}
Run Code Online (Sandbox Code Playgroud)

目前,ConfigurationManager.AppSettings["rootWebDirectory"]指向绝对路径.我相信这是我的问题所在.我无法弄清楚如何将所有这些切换到相对路径.

谢谢!

asp.net-mvc azure

12
推荐指数
2
解决办法
2万
查看次数

Node.js - 在Dev中自动刷新

我正在尝试改善我的节点中的DEV体验.为此,我想:

a)在更改服务器端代码时重新启动服务器
b)在客户端代码更改时刷新浏览器.

为了实现这一目标,我开始将nodemonbrowserSync集成到我的gulp脚本中.

在我的gulp脚本中,我有以下任务:

gulp.task('startDevEnv', function(done) {
    // Begin watching for server-side file changes
    nodemon(
        { script: input.server, ignore:[input.views] })
        .on('start', function () {
            browserSync.init({
                proxy: "http://localhost:3002"
            });
        })
    ;    

    // Begin watching client-side file changes
    gulp.watch([ input.css, input.js, input.html ], function() { browserSync.reload(); });
    done();
});
Run Code Online (Sandbox Code Playgroud)

运行上述任务后,我的浏览器将打开http:// localhost:3000 /.我的应用程序按预期显示.但是,在控制台窗口中,我注意到:

Error: listen EADDRINUSE :::3002
Run Code Online (Sandbox Code Playgroud)

我在某种程度上理解.我有app.set('port', process.env.PORT || 3002);我的server.js文件.然而,我认为这是设置代理值的目的.不过,每当我进行代码更改时,我在控制台窗口中都会看到以下相关错误:

[07:08:19] [nodemon] restarting due to changes...
[07:08:19] [nodemon] starting `node ./dist/server.js` …
Run Code Online (Sandbox Code Playgroud)

javascript node.js nodemon gulp browser-sync

12
推荐指数
2
解决办法
4250
查看次数

替换AngularJS中的jQuery'on'函数

我有一些遗留的jQuery代码,如下所示:

$(document).on('replace', 'div', function (e, new_path, original_path) {
  // do stuff here
});
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何将此代码转换为AngularJS一致的方法.当index.html被加载时,上面的代码正在运行.我正在尝试将初始化代码移动到指令中.目前,我正在调用如下所示的指令:

<body initialize-page>
  ... content goes here
</body>
Run Code Online (Sandbox Code Playgroud)

我的指令如下所示:

.directive('initializePage', function ($document) {
    return {
        restrict: 'A',
        link: function (element) {
            console.log('initialization code goes here.');
        }
    };
})
Run Code Online (Sandbox Code Playgroud)

但是,我不知道AngularJS相当于'on'是什么.如果可能的话,我想摆脱使用jQuery.

谢谢!

angularjs

11
推荐指数
1
解决办法
4426
查看次数