我有一个SelectList,它填充了数据库中的数据.数据来自a Dictionary<int, string>并填充SelectList,如下所示.
//...
Courses = new SelectList(GetCourses(),"Key","Value");
//...
Run Code Online (Sandbox Code Playgroud)
在视图上,它将数据库中的第一条记录显示为下拉列表中的默认选定值.我想清除此值并将下拉列表留空,以便用户可以手动选择一个值.如果可能的话,不使用javascript.
//I have this on the view
<%= Html.DropDownListFor(model => model.CourseID, Model.Courses) %>
Run Code Online (Sandbox Code Playgroud)
请帮忙!
我正在使用ASP.NET MVC 3应用程序,它在使用默认路由时工作正常.所以我有类似这样的网址,localhost:4457/Users/Details/5
我想在url中添加一个友好的slug使其成为localhost:4457/Users/Details/5/some-ones-name.
所以我添加了一个自定义路线,我的路线现在看起来像这样
routes.MapRoute(
"withslug",
"{controller}/{action}/{id}/{slug}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, slug = UrlParameter.Optional });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
在视图中我有这样的动作链接
@Html.ActionLink(Model.Name,"Details","Users", new { id=Model.ID, slug="some-ones-name"},null)
Run Code Online (Sandbox Code Playgroud)
我也有像这样的图像链接
<img src="../../Content/images/picture.jpg" alt="" height="42" width="37" />
链接工作正常,当我点击它正确后.该地址读取localhost:4457/Users/Details/5/some-ones-name.问题是页面上的所有图像现在都被破坏了,我的javascript函数将无法运行.
如果我从地址中删除slug并将url保留为localhost:4457/Users/Details/5一切都按预期工作.
如果我在id之后添加一个斜杠,一切都会再次中断.内容按预期加载,只是图像和一些javascript函数.
我不明白问题是什么,任何见解都将受到高度赞赏.
我需要帮助初始化c#中的静态只读变量.我有一个有这个签名的课程
public class AgentDescriptions
{
public static readonly int P1;
public static readonly int P2;
static AgentDescriptions()
{
int agencyID = 1; //I need to pass this in the constructor somehow
P1 = GetIDFromDB(agencyID);
P2 = GetIdFromDB(agencyID);
}
}
Run Code Online (Sandbox Code Playgroud)
P1和P2在应用程序中反复使用,我试图避免两件事.1)每次我需要使用P1和P2时,幻数和2)跳转到DB.
在应用程序中,我以这种方式在许多地方使用它们
if (something == AgentDescriptions.P1)
//Blah();
Run Code Online (Sandbox Code Playgroud)
请帮忙.如何在静态构造函数中传递agencyID?如果我添加另一个构造函数并在那里传递agencyID,我每次使用它时都必须初始化该类吗?这是否意味着每次都要去DB?