小编Eri*_*sch的帖子

C#foreach循环项软"破"

假设我有一个foreach循环来对List MyTypeInstanveVariable执行一些处理

foreach(MyType item in MyTypeInstanceVariable) {
    if (cond1) {}

    if (cond2) {}
}
Run Code Online (Sandbox Code Playgroud)

如果满足cond1,我想前进到MyTypeInstanceVariable中的下一个项目.现在我可以添加一个布尔值来检查是否评估cond2,但是是否有一些内置命令可以快速移动到下一个项目?我不能使用break,因为它会彻底打破foreach循环

谢谢托马斯

c# foreach skip

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

UIHint不使用EditorTemplate

我有这样的模型:

public class MyModel {
    [ScaffoldColumn(false)]
    public int CharityId { get; set; }

    [UIHint("Charities")]
    public SelectList Charities { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个名为Charities.cshtml的EditorTemplate:

@model MyModel

@Html.DropDownListFor(model => model.CharityId, Model.Charities)
Run Code Online (Sandbox Code Playgroud)

然后在我的页面中:

@model MyModel

@Html.EditorForModel()
Run Code Online (Sandbox Code Playgroud)

但是,无论如何,它都不会呈现慈善机构模板.我一直在抨击我的大脑,它应该工作......但事实并非如此.

有任何想法吗?

编辑:

问题是,默认的对象模板将不会呈现比本身(所谓的小角度俯冲)等复杂的对象,即使是有UIHint.我的印象是UIHint会让它渲染模板,但我显然是错的.

修复是不要试图让模型自己渲染.那很难过.

c# asp.net-mvc asp.net-mvc-3 view-templates

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

MVC4 SelectList未选择默认对象

我的选择列表没有选择通过代码引入的默认对象.

我首先创建我SelectList喜欢的:

public SelectList CreateSelectList(object objSelected = null)
{
  return new SelectList(GetAll().OrderBy(s => s.NumericalValue), "PeriodID", "Name", objSelected);
}
Run Code Online (Sandbox Code Playgroud)

我的objSelected充满了与PeriodID相关联的Guid.

在我的控制器内部,我将我的viewbag变量定义到新的选择列表.

public ActionResult Edit(Guid id)
{
  Classroom classroom = classroomRepository.GetByID(id);
  ViewBag.PeriodID = periodRepository.CreateSelectList(classroom.PeriodID);
  return View(classroom);
}
Run Code Online (Sandbox Code Playgroud)

然后在我的视图中这是我如何显示我的SelectList:

<div class="control-group">
  @Html.LabelFor(model => model.PeriodID, "Period", new { @class = "control-label" })
  <div class="controls">
      @Html.DropDownListFor(model => model.PeriodID, ViewBag.PeriodID as SelectList, String.Empty, new { @class = "span3" })
      @Html.ValidationMessageFor(model => model.PeriodID)
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc selectlist

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

从通用列表中删除项目

我试图使用RemoveAt从通用列表中删除项目.奇怪的是,在使用调试器时,我可以看到我的项目已被删除,但在将其传递给视图时,删除的项目正在显示,但最后一项已被删除.

代码看起来像这样

public ActionResult(MyModel model, int[] removeitems)
{
 //model.ListItems has 10 items
 //Incoming removeitems has 0 as the first item to remove as a test
foreach(int item in removeitems)
{
 model.ListItems.RemoveAt(item);
}
 //by this time debugger shows that item 0 has in fact been removed and no longer exists in the list
 return View(model);
 //after the view is rendered it shows item 0 is still there but 10 has been removed
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过另一种方式将项目复制到另一个列表等,但所有测试显示上面的代码确实删除了第一个项目,但视图没有反映这一点.

有任何想法吗?

c# asp.net asp.net-mvc

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

调用web API字符串参数

我有这个功能

[System.Web.Http.HttpGet]
[System.Web.Http.ActionName("StartProcess")]
public object StartProcess(string items)
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

试着打电话

$.ajax({
     url: '/api/Details/StartProcess',
     type: 'get',
     contentType: 'application/json',
     data: items,
     success: function() {
         logger.log('Successful', "", "", true);
     },
     error: function(error) {
         var jsonValue = jQuery.parseJSON(error.responseText);
     });
Run Code Online (Sandbox Code Playgroud)

继续得到404错误.我的其余调用工作,但这是我需要发送参数的第一个.

items只是逗号分隔的字符串.

这是我的路线信息.

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id=Urlameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

我缺少什么想法?

ajax asp.net-mvc asp.net-web-api

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


在ASP.NET 5类库中找不到System.Math命名空间

我试图建立一个ASP.NET 5类库,它使用一些方法从 System.Math,即System.Math.Round(...),System.Math.Sqrt(...) 等等.

当我尝试构建它时.我收到以下错误:

Error   CS0103  The name 'Math' does not exist in the current context   ProjectName.ASP.NET Core 5.0    Class1.cs
Run Code Online (Sandbox Code Playgroud)

我已经包括在project.json文件中的一些包来修复其他错误,与System.LinqSystem.Collections.Generic,但我不精包含一个包System.Math.

我在哪里可以找到它?

这是我的project.json文件:

"version": "1.0.0-*",
"dependencies": {
},
"frameworks" : {
    "aspnet50" : { 
        "dependencies": {
        }
    },
    "aspnetcore50" : { 
        "dependencies": {
            "System.Runtime": "4.0.20-beta-22416",
            "System.Collections": "4.0.10-beta-22416",
            "System.Linq": "4.0.0-beta-22416",
            "System.Threading": "4.0.0-beta-22416"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net class-library asp.net-core

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

Specified cast is not valid C# Entity Framework

Hi I have been getting this Specified cast is not valid in my code but when I connect to my backup database I do not get the Specified cast is not valid error. I am not sure what is going on.

Again, this code works perfectly on one database and gives me the error on the other.

My Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Linq.SqlClient;
using System.Text.RegularExpressions;
using MvcPaging;
using lookups.Models;
using lookups.Helpers;


namespace …
Run Code Online (Sandbox Code Playgroud)

c# sql asp.net-mvc entity-framework casting

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

错误:转换为值类型'System.Int32'失败,因为具体化值为null

我遇到了问题

转换为值类型'System.Int32'失败,因为实现的值为null.结果类型的泛型参数或查询必须使用可空类型.

我已经找到了解决问题的答案,但我找不到相似的案例.

我试图从输入中找出max交易ID .如果帐户没有任何交易,则会导致错误.这两个表之间的关系为tblTransaction.accountId = tblAccount.IdtblTransactionsaccount id

我的控制器:

//GET: Balance

    [Authorize]
    public ActionResult Index()
    {
        string useracc = (string)Session["AccountNumber"];

        var accountInstance = db.Accounts.FirstOrDefault(w => w.AccountNumber.ToString() == useracc);


        List<Transaction> AccountTransactions = db.Transactions.Where(w => w.AccountId == accountInstance.Id&&w.IsCancelled ==false).Select(w => w).OrderByDescending(w => w.Date).ToList();

            var accountStatement = AccountTransactions.Where(w => w.TransactionTypeId == 2 && w.Date.Year >= 2015).OrderByDescending(w => w.Date).ToList();

            var lastTransactionId = db.Transactions.Where(w => w.AccountId == accountInstance.Id && w.IsCancelled == false && w.TransactionTypeId == 2 && w.Date.Year >= …
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc entity-framework

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

RSpec升级到OSX Lion后停止工作

dyld: Library not loaded: /System/Library/Frameworks/OpenCL.framework/Versions/A/Libraries/libclparser.dylib
  Referenced from: /opt/local/bin/identify
  Reason: image not found
Run Code Online (Sandbox Code Playgroud)

有没有人有办法解决吗?将Xcode升级到4.1有帮助吗?

ruby macos rspec ruby-on-rails osx-lion

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