小编Ser*_*rge的帖子

如何在MVC 4中为枚举创建默认编辑器模板?

我们知道,如果我们为基类型定义模板,那么该模板也可以用于派生类型(如果没有使用任何其他模板来覆盖它).

因为我们不能继承一个Enum,也不会enum被认为是继承的Enum,所以对于对象的不同自定义枚举属性,它们中的Enum.cshtml模板Views\Shared\EditorTemplates都不会处于活动状态,如下所示:

public enum Role
{
    Admin,
    User,
    Guest
}
Run Code Online (Sandbox Code Playgroud)

我已经在ASP中看到了关于这个主题的一些答案,但是我想知道在MVC 4中是否对这个主题有一些改进?

PS.我的意思是使用任何明确的模板归属(如@Html.EditorFor(model => model.Role, "Enum")[UIHint("Enum")])

PPS.我是MVC的新手,所以我很感激你的简单答案.

c# asp.net-mvc enums templates asp.net-mvc-4

2
推荐指数
1
解决办法
6843
查看次数

在服务/工厂中使用$ http.get来返回集合

我尝试http.get在angularjs服务中使用promise,对获得的集合进行一些操作,最后将其返回给控制器......

我的问题是如何$http.get()在服务中使用a 来获取和操作结果,然后将其返回给控制器,如下面的代码所示: PEN代码

var app = angular.module('myApp', []);

app.controller('customersCtrl', ['$scope','customer',function($scope, customer) {
  $scope.odds = customer.odds;
}]);

app.factory('customer', ['$http', function($http) {
  var all = [{'Id':88, 'Name':"A"}, {'Id':89, 'Name':"ShoutNotBeHere"}]; 
  var odds = [];

  $http.get("http://www.w3schools.com/angular/customers.php")
    .then(function(response) {
      all = response.records;
    });

  angular.forEach(all, function(c, i) {
    if (i % 2 == 1) {
      odds.push(c);
    }
  });

  return {odds: odds};
}]);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
  <div ng-app="myApp" ng-controller="customersCtrl">
    Odd ids from www.w3schools.com/angular/customers.php
    <ul>
      <li ng-repeat="c in odds">
        {{ c.Id …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-factory angularjs-http angular-promise

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

使用对象列表将ID数组映射到字符串数组

var myIds = [3, 4, 2];

var myObj = [
    {id:1, name:'one'}, 
    {id:2, name:'two'},
    {id:3, name:'tree'},
    {id:4, name:'four'}];

// need to obtain ['tree', 'four', 'two']

var idsToNames= function(ids, objects) {
    var myNames = myIds.map(function(id){
        // transform id to name
        foreach(o in objects){
            if (i.id == id) 
                return o.name;
        }
    });
    return myNames;
}
Run Code Online (Sandbox Code Playgroud)

它是将id数组转换为名称数组的最佳方法吗?

javascript arrays object

2
推荐指数
1
解决办法
1226
查看次数

使标记帮助程序像“ a”一样“按钮”工作

在ASP.NET Core中,我可以将操作设置为链接项a,但是如果将其更改为按钮,则该操作将不再起作用。

将动作/控制器绑定到button点击的正确方法是什么?

<a asp-action="Delete" asp-route-id="@item.Id">remove</a> @*work*@
Run Code Online (Sandbox Code Playgroud)

<button asp-action="Delete" asp-route-id="@item.Id">remove</button> @*does not work*@
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core

2
推荐指数
1
解决办法
2731
查看次数

ASP .NET Core 和 Azure 表存储

我是ASP.NET Core 的新手,我们必须实现一个应用程序来存储一些非关系数据(类似 Excel 的表中的某些行),因此我们决定使用 Azure 表。据我了解,EntityFramework Core不支持AzureTables ......在这种情况下,正确的方法是什么?

azure azure-table-storage asp.net-core

2
推荐指数
1
解决办法
4438
查看次数

访问ASP.NET Core中的配置选项:选项模式

我有一个ASP.NET核心应用程序.我尝试使用选项模式,但它似乎不起作用.

我有以下内容appsettings.json:

{
    "ConnectionStrings": {
        "MyTablesConnectionString": "Default[...];EndpointSuffix=core.windows.net"
    },

    "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" }  }
}
Run Code Online (Sandbox Code Playgroud)

以下ConnectionStrings课程

public class ConnectionStrings {
    public ConnectionStrings()  {
        MyTablesConnectionString = "default value";
    }

    public string MyTablesConnectionString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();

    // HERE IS THE GOOD VALUE !!!!!!!!
    Debug.WriteLine($"Connection string is:{Configuration["ConnectionStrings:MyTablesConnectionString"]}"); …
Run Code Online (Sandbox Code Playgroud)

c# configuration .net-core asp.net-core

2
推荐指数
1
解决办法
1417
查看次数

从控制台应用程序接收文件时,IFormFile始终为null

我已经准备好WebApi方法来接受文件,如下所示:(例如uri“ https:// localhost:44397 / api / uploadfile

 [Route("api/[controller]")]
 [ApiController]
 public class UploadFileController : ControllerBase
 {
    [HttpPost]
    public async Task<IActionResult> Post([FromForm] IFormFile file)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用控制台应用程序将文件发送到此api方法,以下是我的代码:

    public static void Send(string fileName)
    {
        using (var client = new HttpClient())
        using (var content = new MultipartFormDataContent())
        {
            client.BaseAddress = new Uri("https://localhost:44397");
            var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            var index = fileName.LastIndexOf(@"\");
            var fn = fileName.Substring(index + 1);
            fs.Position = 0;

            var contentfile = new StreamContent(fs);
            content.Add(contentfile, "file", fn);
            var result …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-web-api2 .net-core asp.net-core-webapi-2.1

2
推荐指数
1
解决办法
1141
查看次数

CSS:有条件地应用一个类

如何仅使用CSS(最糟糕的情况是使用某些JS)实现以下操作

global.css(只读)

.red { 
    color: red;
    /*tens of lines of additionnal css*/ 
}
Run Code Online (Sandbox Code Playgroud)

mystyle.css

div[error] {
    background: yellow; 
    apply .red;                                 << here
}
Run Code Online (Sandbox Code Playgroud)

PS。问题不是关于SASSLESS

javascript css

2
推荐指数
1
解决办法
171
查看次数

在 asp.net core 控制器中获取“请求有效负载”

在客户端,我做

$.ajax({
    url: '/emplacements/keyexist',
    type: "POST",
    data: JSON.stringify(postData),
    dataType: "json",
    traditional: true,
    contentType: "application/json; charset=utf-8",
Run Code Online (Sandbox Code Playgroud)

但是操作方法中的值始终为“null”

[AcceptVerbs("GET", "POST")]
public IActionResult KeyExist(
    string nom,            //[Bind(Prefix = nameof(EmplacementDTO.Nom))],
    int id                 //[Bind(Prefix = nameof(EmplacementDTO.Id))]
)
{
    // nom == null
    // id == 0
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 如何修复它?

c# telerik-grid remote-validation asp.net-core-mvc .net-5

2
推荐指数
1
解决办法
6852
查看次数

全局命名空间和 onclick 函数的污染

假设代码

//(function() {
  function addItem() {
    alert("item added!");
  }
//})();
Run Code Online (Sandbox Code Playgroud)
<button onclick="addItem()">add item</button>
Run Code Online (Sandbox Code Playgroud)

如果我会注意不污染全局命名空间,并取消注释注释代码,我的功能将被破坏......

此类问题的常用解决方法是什么,保持 html 内联“on...”事件处理程序?


附注。作为初学者读者的注意事项:

有经验的开发人员可能会建议避免内联onclick属性。不使用它们的许多原因包括:

  • 表现和行为之间的紧密耦合;
  • 代码(通常)在全局范围内运行;
  • 使测试和调试变得困难;
  • 无视渐进增强;
  • 很快变得难以维护;
  • 它还将取消绑定任何先前分配的点击处理程序,这可能是不需要的副作用;

html javascript

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