小编fea*_*net的帖子

MVC模型绑定到接口

我创建了一个看起来像的OrderFormViewModel

public class OrderFormViewModel 
{
    public IOrderDetails { get; set; }
    public IDeliveryDetails { get; set; }
    public IPaymentDetails { get; set; }
    // ... etc

    public SelectList DropDownOptions { get; set; }
    // ... etc

}
Run Code Online (Sandbox Code Playgroud)

这将转到我的创建视图,其中每个部分(即交付详细信息,付款详细信息等)随后会传递到捕获必要字段的部分视图.

我认为这一切都非常简洁,直到我运行它并且当然意识到MVC模型绑定器不知道如何实例化任何接口.

有办法以某种方式解决这个问题吗?

我也在尝试使用Unity容器学习DI,所以我试图避免在我的UI项目中引用任何具体的类(模型在一个单独的项目中).

c# dependency-injection interface model-binding asp.net-mvc-2

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

ASP.NET中的应用程序生存期

这应该是一个简单的问题,但我没有设法在谷歌找到答案.

我想知道,就白痴可以理解的而言,究竟应用程序生命周期在ASP.NET中意味着什么(因此当你可以期待应用程序启动和结束事件运行时).

我假设它是在IIS中运行和停止应用程序的时候,但我读过的东西表明它与请求数量有关.

asp.net iis web-applications lifetime

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

铸造和Linq Cast <T>()

在尝试回答这个问题时,我发现了以下内容:

string s = "test";

var result1 = s.Select(c => (ushort)c); // works fine

var result2 = s.Cast<ushort>(); // throws an invalid cast exception
Run Code Online (Sandbox Code Playgroud)

为什么在Cast<T>()这里失败?有什么不同?

linq extension-methods linq-to-objects casting

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

ASP.NET MVC使用jQuery ajax渲染局部视图

我有一个控制器动作,呈现局部视图:

public ActionResult Details(int id)
{
    DetailsViewModel model = 
        ModelBuilder.GetDetailsViewModel(id, _repository);
    return PartialView("Details", model);
}
Run Code Online (Sandbox Code Playgroud)

我将返回的内容加载到动态元素中,如下所示:

$container = appendContainer(); // adds a div to the dom with the correct id
$container.load("MyController/Details", function(response, status, xhr) {
    if (status != "success") {
        $(container).html('an error has occured');
    }
});
Run Code Online (Sandbox Code Playgroud)

所以这会创建一个div,然后将返回的内容加载到该div中.

我想略微改变它,以便只有在对控制器的调用成功时才创建容器div.

所以:

  1. jQuery调用控制器动作
  2. controller返回PartialView,如果未找到Id,则返回null
  3. 如果返回PartialView,则会创建容器并使用返回的内容加载容器.
  4. 如果控制器未找到Id,则不会创建任何内容并显示警报.

我很感激有关如何最好地实现这一点的任何指示.

ajax asp.net-mvc jquery partial-views

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

从FormCollection中获取所有选中的复选框

我有一个表单,其中包含一大堆复选框和一些其他类型的控件.我需要检索每个选中复选框的名称.

做这个的最好方式是什么?我可以使用linq查询吗?

所以在伪代码中,我希望做这样的事情:

var names = formCollection
               .Where(c => c is Checkbox && c.Checked)
               .Select(c => c.Name);
Run Code Online (Sandbox Code Playgroud)

更新似乎MVC提交复选框的方式与普通表单的行为方式不同,因为隐藏字段也会呈现.我在这里找到了详细信息:如何处理ASP.NET MVC表单中的复选框?

Anywho,我已经得到了该线程的帮助以及下面BuildStarted的答案.以下代码完成了这一操作.

var additionalItems = form.AllKeys
       .Where(k => form[k].Contains("true") && k.StartsWith("addItem"))
               .Select(k => k.Substring(7));
Run Code Online (Sandbox Code Playgroud)

checkbox asp.net-mvc controls webforms formcollection

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

使用FileResult返回压缩文件的ASP.NET MVC缺少扩展名

我有一个压缩文件,我试图流到客户端:

public FileResult GetFiles()
{
    return File("test.zip", "application/zip");
}
Run Code Online (Sandbox Code Playgroud)

文件下载但没有".zip"扩展名.

asp.net-mvc streaming fileresult

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

比较两个SQL数据库

我在开发机器上做了一大堆更改(表定义,存储过程等).它有一个脚本或我可以运行的免费工具,它将找到开发和实时服务器之间的所有差异?

sql sql-server-2008 database-schema

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

C#中的"var"类型推断

可能重复:
为什么var在"foreach(table.Rows中的var row)"中评估为System.Object?

今天发现以下内容我感到非常惊讶....

SqlDataReader reader = cmd.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();


// the following compiles correctly
foreach (DataRow field in schemaTable.Rows)
{
    Console.WriteLine(field["ColumnName"]);
}


// the following does not compile as 'var' is of type 'object'
foreach (var field in schemaTable.Rows)
{
    // Error: Cannot apply indexing with [] to an expression of type 'object'
    Console.WriteLine(field["ColumnName"]);
}
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

这是一种类型推断失败吗?如果是这样,是什么原因造成的?

或者它是定义的行为的一部分还是var?如果是这样,为什么?

我认为这个想法var是你可以在变量声明/初始化的任何地方使用它而不改变行为.

.net c# var type-inference

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

无法加载文件或程序集...数组下标超出范围

我之前很好的asp.net项目突然开始在@PAGE指令下对于ASP.NET运行时错误抱怨:无法加载文件或程序集...等我尝试时遇到以下错误和调试.我已经尝试了所有非常明显的东西...清理,重建,删除临时asp.net文件,删除和读取参考,重新启动视觉工作室,重新启​​动我的电脑等我完全难倒,我找不到任何在谷歌上使用的东西.有谁知道发生了什么事?

Index was outside the bounds of the array. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the …
Run Code Online (Sandbox Code Playgroud)

asp.net

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

如何在代码中告诉我们是否从testmethod .net测试运行

有没有办法知道何时从运行测试方法调用代码?

bool MyMethod()
{
    if ( /* are we running a test? */ )
    {
        return true; // otherwise this will fail from the automated build script
    }
    else
    {
        // run the proper code
    } 
}
Run Code Online (Sandbox Code Playgroud)

请饶恕我"这是一个非常糟糕的主意"评论:)

.net c# testing

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