我正在学习如何在ASP.NET MVC中创建自定义路由并且遇到了障碍.在我的Global.asax.cs文件中,我添加了以下内容:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
}
Run Code Online (Sandbox Code Playgroud)
我的想法是能够导航到http://localhost:123/home/filter/mynameparam.这是我的控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Filter(string name)
{
return this.Content(String.Format("You found …Run Code Online (Sandbox Code Playgroud) 我先使用EF 4.1代码.给出以下类片段:
public class Doctor
{
public virtual ICollection<Hospital> Hospitals { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
注意:我在数据库上下文中有这个:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
this.Configuration.LazyLoadingEnabled = false;
}
Run Code Online (Sandbox Code Playgroud)
我想确保这里不涉及延迟加载.
我的问题是,如果没有virtualHospitals属性上的关键字,当我找回一家医院与他有关联的医院时,该收集是空的.通过包含virtual关键字,医院集合确实包含1项,这是我所期望的.
问题是,当我想创建一个全新的医生并立即将他与医院联系时,我得到一个Null reference例外,因为医院的财产尚未初始化.
谁能指出我在这里做错了什么?如何在创建新医生时向医院添加项目.
干杯.雅.
我有select一个通过KnockoutJS视图模型填充的HTML 元素,如下所示:
<select data-bind="options: $data.answers, optionsText: function(item) { return item.text }, value: selectedAnswer, optionsCaption: 'Choose...'"></select>
Run Code Online (Sandbox Code Playgroud)
我想做的是通过某种方式设置class每个option元素的属性,以便最终得到:
<option class="someValue">12345</option>
Run Code Online (Sandbox Code Playgroud)
可以在KnockoutJS中完成吗?我很难为此找到解决方案。
编辑:
foreach正如Artem所建议的,我刚刚尝试了绑定,它非常接近我想要的工作。但是有一个问题。
在Question对象上有一个订阅SelectedAnswer可观察对象的函数:
this.selectedAnswer.subscribe(function (answer) {}
使用foreach绑定时,subscribe()每个问题都会触发一次(在我选择答案之前不应该触发。我认为这是因为现在没有显示“选择...”选项)。
对于foreach绑定,我如何才能将默认文本设置回“ Choose ...”,并停止selectedAnswer.subscribe()触发直到用户选择了一个项目为止,而不是在填充列表时才停止。
编辑:
好的,这就是我的操作方式。
在KO viewmodel类中,我有一个布尔值bindingFinished,我在中检查selectedAnswer.subscribe()。如果是的false话,我们就从函数返回;如果为true,则照常进行。
另外,我通过Answer在数组的开头添加新的选项来为答案添加默认的“选择”选项。最终结果是,仅在用户选择了选项之后,才在绑定数据时不执行订阅功能。
非常感谢Artem的帮助。
当该参数为字典类型时,如何为操作设置默认参数值?
例如:
public void Test(Dictionary<int, bool> dic) {
...
}
Run Code Online (Sandbox Code Playgroud) 我正在使用MS Fakes。
进行以下课程:
public class Person
{
public void SaveQuotes()
{
DoSomething();
}
private void DoSomething()
{
Console.WriteLine("Original DoSomething called.");
}
}
Run Code Online (Sandbox Code Playgroud)
和这个测试:
[TestMethod]
public void TestMethod1()
{
using (ShimsContext.Create())
{
var sut = new ShimPerson();
sut.DoSomething = () => { Console.WriteLine("Called from test"); };
sut.Instance.SaveQuotes();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,该sut.Instance.SaveQuotes()方法基本上被跳过了,因为该方法已被填充。我想要的是执行原始SaveQuotes()方法。所以我尝试了这个:
[TestMethod]
public void TestMethod1()
{
using (ShimsContext.Create())
{
var sut = new ShimPerson();
sut.DoSomething = () => { Console.WriteLine("Called from test"); };
sut.SaveQuotes = () => { …Run Code Online (Sandbox Code Playgroud) 我需要选择倒数第二个输入可选元素的值:
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
Run Code Online (Sandbox Code Playgroud)
选择输入标签位于tr标签内.
如果我使用$("select.x").last(),jQuery选择最后一个元素.我需要选择倒数第二名.
我正在开发一个使用Oracle的"Siebel CRM"产品的C#项目.Siebel提供了两个名称空间和类,用于以编程方式连接到它的服务,具体取决于您是要连接到服务器还是连接到客户端会话:
SiebelBusObjectInterfaces.SiebelDataControl
TWSiebelLib.SiebelWebApplication
Run Code Online (Sandbox Code Playgroud)
SiebelDataControl公开了一种显式登录基于服务器的资源的特定方法:
bool SiebelDataControl.Login(string connString, string userName, string passWord)
Run Code Online (Sandbox Code Playgroud)
所有其他方法在两个类之间是相同的..
我目前正在维护我的两个版本的应用程序 - 每个版本对应一个不同的连接类.随着我构建的功能越来越具有包容性和复杂性,将这两个应用程序分开维护变得非常困难.我已将连接部分拆分为它自己的类,但我仍然需要在开发时在应用程序之间复制和粘贴代码.
理想情况下,我想要一个VS解决方案,它有一个我所有自定义代码的实例和一些逻辑,用于确定用户决定使用哪两个连接类.
从理论上讲,我想要的是:
class SiebelAppWrapper {
public Object m_SiebelAppInstance;
private Boolean m_isConnected;
private short m_conType;
public static short SERVER_CON = 1;
public static short CLIENT_CON = 2;
public SiebelAppWrapper(short conType) {
if (conType == SERVER_CON) {
m_SiebelAppInstance = new SiebelDataControl();
} else if conType == CLIENT_CON {
m_SiebelAppInstance = new SiebelWebApplication();
}
m_conType = conType;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,由于显而易见的原因,编译器对此并不满意.
我对C#和.NET开发一般都很陌生,所以我希望有一些方法可以在不复制所有dependend代码的情况下合并这两个类.
非常感谢您的帮助.
//Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/");
request.Credentials = new NetworkCredential("pvYMExk3QIO7p2YUs6BBkg", "rO3DsucETRadbbfxHkd6qw");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//WRITE JSON DATA TO VARIABLE D
string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\"6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3f644b15dd05ac\"]}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set …Run Code Online (Sandbox Code Playgroud) 如何配置SpecFlow,使其不会将时序信息显示为测试文本的一部分,例如
- > done:Steps.ThenIWillBeDeniedAccess()(0.0s)
干杯.雅.
我已经开始使用TransactionScope来帮助我进行单元测试,以便将我的测试数据库恢复到之前的状态.使用这个与SpecFlow,我有一个像这样的基类:
public class TransactionScopedFeature
{
private TransactionScope Scope { get; set; }
[BeforeScenario]
public void BaseSetup()
{
this.Scope = new TransactionScope(TransactionScopeOption.RequiresNew);
}
[AfterScenario]
public void BaseCleanup()
{
if (this.Scope != null)
{
this.Scope.Dispose();
}
}
}
Run Code Online (Sandbox Code Playgroud)
所有上述工作,当我向数据库添加记录时,当我在测试完成后查询表时,这些表是空的.伟大的东西,非常聪明!
我的问题与这些表中的标识列有关.我注意到的是,当我多次运行我的测试时,我的测试表的ID列增加1.我假设由于TransactionScope将回滚更改,因此身份种子也将被回滚.
我做错了这个假设 - 这就是数据库的工作原理吗?如果是这种情况,我也可以在每个执行此操作的方案之前运行SQL脚本:
DBCC CHECKIDENT ('dbo.Items', reseed, 0)
我只是想检查一下我做错了什么,或者这是正常的数据库行为.
干杯.雅.
c# ×3
.net ×2
parameters ×2
asp.net-mvc ×1
database ×1
javascript ×1
jquery ×1
knockout.js ×1
methods ×1
specflow ×1
transactions ×1
unit-testing ×1