小编lea*_*ing的帖子

简单的愚蠢的foreach循环问题

在下面的代码中,我试图阅读所选项目的列表和所有可用项目的列表.

我想存储所有未从所有项目列表中选择的项目.但是使用下面的代码,我有所有项目的列表...在哪里以及如何打破第二个循环incase projectId.Value == selectedProjectId.Value?

public IEnumerable NotselectedProjects; public IEnumerable NotSelectedProjects(){

        if (this.NotselectedProjects == null)
        {
            List<SelectListItem> result = new List<SelectListItem>();
            foreach (var selectedProjectId in selectedProjects) 
            {
                foreach (var projectId in projectLists)
                {
                    if (projectId.Value != selectedProjectId.Value)
                    {
                        result.Add(new SelectListItem
                        {
                            Selected = false,
                            Text = projectId.Text,
                            Value = projectId.Value
                        });
                        this.NotselectedProjects = result.AsEnumerable();
                    }
                }
            }
        }
        return this.NotselectedProjects;

    }
Run Code Online (Sandbox Code Playgroud)

c#

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

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

如何在销毁后重置对话框中的文本框值?

我有一个对话框,其中有 2 个文本框。我想在对话框被销毁后重置文本框值。我怎样才能做到这一点?

如果我在文本框中输入了值并单击“取消”,重新打开对话框将显示之前输入的数据。

function setDialogWindows($element) {
      $('#change').dialog({    
           autoOpen: true,    
           width: 380,    
           buttons: {   
              "Cancel": function() {    
                   $(this).dialog('destroy');   
              },    
              "Accept": function() {    
              } 
     }): 
 }
Run Code Online (Sandbox Code Playgroud)
$('#link').click(function() {    
    setDialogWindows('#change');    
});
Run Code Online (Sandbox Code Playgroud)
<div id="change" title="Change password" >
    <input type="hidden" id="User_Name" name="Name"/>    
    <input type="textbox" id="text1" />
    <input type="textbox" id="text2" />
</div>
Run Code Online (Sandbox Code Playgroud)

jquery

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

单元测试未达到一段代码

我是单元测试的新手.我想做点如下的事情:

[Test]
[ExpectedException(ExceptionType = typeof(Exception))]
public void TestDeleteCategoryAssociatedToTest()
{
    Category category = CategoryHelper.Create("category", Project1);
    User user;
    Test test1 = IssueHelper.Create(Project1, "summary1", "description1", user);
    test1.Category = category;
    category.Delete(user);          
    Assert.IsNotNull(Category.Load(category.ID));
    Assert.IsNotNull(Test.Load(test1.ID).Category);
}
Run Code Online (Sandbox Code Playgroud)

我的目标是测试类别没有被删除Assert.IsNotNull()...但由于它抛出异常,它没有达到那段代码.知道如何改进上述测试吗?

实际上在我的API中,如果类别与Test相关联,我会抛出异常......我的代码片段是:

 IList<Test> tests= Test.LoadForCategory(this);
 if (tests.Count > 0)
 {
     throw new Exception("Category '" + this.Name + "' could not be deleted because it has items assigned to it.");
 }
 else
 {
     base.Delete();
     foreach (Test test in tests)
     {
         test.Category = null;
     }
 }
Run Code Online (Sandbox Code Playgroud)

c# unit-testing

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

列表回发时的绑定难度

我很难回复输入的新数据.尽管在提交之前对数据进行了更改,但似乎发送到视图的数据仍会发送回控制器.

我的代码如下:

控制器公共类GroupRateController:Controller {// // GET:/ GroupRate /

    public ActionResult Index()
    {
        GroupRateModel model = new GroupRateModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(GroupRateModel model)
    {
        model.Save(model);
        return View(model);
    }

}
Run Code Online (Sandbox Code Playgroud)

视图

 @model MvcApplication1.Models.GroupRateModel

@{
    View.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@using (Html.BeginForm())
{

@Html.ValidationSummary()

<table>
<thead>

</thead>
<tr><th>Rate Group</th><th>Default Amount</th><th>Client Amount</th></tr>
@foreach (var item in @Model.ClientRateDetails)
{
<tr><td>@item.RateGroupName</td><td align="right">@Html.DisplayFor(m => @item.RateGroupID)</td><td>@Html.EditorFor(model => item.ClientRate)</td></tr>

}
</table>

<p> <input type ="submit"  value="Save" id="submit" /></p>
}
Run Code Online (Sandbox Code Playgroud)

模型

    using System.ComponentModel.DataAnnotations;

namespace …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-3

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

如何将模型传递给具有其他参数的动作

我的问题是当我通过DeleteEmployee中的catch部分时,如何将ModelStateErrors发送到Actionee动作

   public ActionResult Employee(int ID, string Name)
    {
        EmployeeListModel model = new EmployeeListModel (ID, projectName);
        return View(model);
    }

 public ActionResult DeleteEmployee(Employee emp)
    {

        try
        {
            emp.Delete();
            return RedirectToAction("Employee", new { ID = emp.ID, Name = emp.Name });
        }

        catch (Exception e)
        {
            EmployeeListModel model = new EmployeeListModel (emp.ID, emp.Name);
            ModelState.AddModelError("Error", e.Message);
            return RedirectToAction("Employee", model);
        }
    }
Run Code Online (Sandbox Code Playgroud)

使用return View("Employee",model); 我仍然无法将ID和Name作为参数发送.

asp.net-mvc asp.net-mvc-3

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

如何从不同的表中检索数据

我有2个表如下:

Time: ID, UserID, start, End
User: ID, ClientID
Run Code Online (Sandbox Code Playgroud)

我想检索与具有客户端ClientID一段时间的用户相关的所有时间.

例如

与客户端ClientID = 5和Time startdate> 15/12/12 Table Time值相关联的时间

1, 3, 17/12/12 , 18/12/12
2, 5, 16/12/12 , 18/12/12
3, 4, 19/12/12 , 20/12/12
Run Code Online (Sandbox Code Playgroud)

表用户值

1, 4
2, 3
3, 5
4, 5
Run Code Online (Sandbox Code Playgroud)

结果应该是:

1, 3 17/12/12 , 18/12/12
3, 4, 19/12/12 , 20/12/12
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现上述目标?

sql

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

如何包含内部方法

我不想使用[ExpectedException(ExceptionType = typeof(Exception),ExpectedMessage ="")]而是希望在我的方法中包含异常.我能这样做吗?请举个例子.

谢谢

nunit unit-testing

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

异常捕获

有什么区别

 try
 {
     ... 
 }
 catch (NHibernate.ADOException exception)
 {}
Run Code Online (Sandbox Code Playgroud)

try
{
    ... 
}
catch (exception ex)
{}
Run Code Online (Sandbox Code Playgroud)

c# exception-handling

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

使用ASP.NET MVC ViewBag和DropDownListfor时遇到困难

我的困难是如何使用ViewBagDropdownListFor

在我的控制器中,我有:

TestModel model = new TestModel();
ViewBag.Clients = model.Clients;
ViewBag.StatusList = model.StatusList;
ViewBag.enumStatus = model.enumStatus;
ViewBag.intClient = model.intClient;
Run Code Online (Sandbox Code Playgroud)

在我的TestModel中

public SelectList Clients { get; set; }       
public SelectList StatusList { get; set; }
public ActiveStatus enumStatus { get; set; }
public int? intClient { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我看来

我想用来DropDownListFor显示ViewBag值,我该怎么做?

asp.net-mvc asp.net-mvc-3

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