我在使用用户和角色为数据库播种时遇到了问题.
创建用户和角色(我可以在抛出错误后在数据库中看到它们).
但是,当我尝试检查用户是否在角色中时,我得到一个例外.
我的代码是:
public class tbInitializer<T> : DropCreateDatabaseAlways<tbContext>
{
protected override void Seed(tbContext context)
{
ApplicationDbContext userscontext = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(userscontext);
var userManager = new UserManager<ApplicationUser>(userStore);
var roleStore = new RoleStore<IdentityRole>(userscontext);
var roleManager = new RoleManager<IdentityRole>(roleStore);
if(!userscontext.Users.Any(x=> x.UserName=="marktest"))
{
var user = new ApplicationUser { UserName = "marktest", Email = "marktest@gmail.com" };
userManager.Create(user, "Pa$$W0rD!");
}
if (!roleManager.RoleExists("Admin"))
{
roleManager.Create(new IdentityRole("Admin"));
}
if(!userManager.IsInRole("marktest","Admin"))
{
userManager.AddToRole("marktest","Admin");
}
Run Code Online (Sandbox Code Playgroud)
但是,在线:
if(!userManager.IsInRole("marktest","Admin"))
抛出错误的异常: UserId not found.
在抛出异常后检查时,用户和角色都在数据库中:
谁能看到我做错了什么?
谢谢你的帮助,
标记
我使用Google Chrome V28作为我的浏览器 - 我的模型上的DataAnnotations存在问题,这会迫使Chrome在我的视图中呈现数据时类型时使用它自己的内置日历.
我的模型是:
public class ObScore
{
public int ObScoreId { get; set; }
...
...
[DisplayFormat(DataFormatString = "{0:dd MMMM yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date")]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
...
...
}
Run Code Online (Sandbox Code Playgroud)
模型中肯定有数据:
...但在"修改"模式下显示时,Chrome会显示:
这是Chrome中的已知错误,我可以在代码中执行任何操作,将日期强制转换为表单吗?
谢谢,马克
是否可以在下面更改我的查询,以便它使用types
包含类型查询中的列表.
所以不要:
var cust = db.Customers.Where(x => x.type_id==9 || x.type_id==15 || x.type_id==16).ToList();
Run Code Online (Sandbox Code Playgroud)
......我会有类似的东西:
List<int> types = new List<int> { 9, 15, 16 };
var cust = db.Customers.Where(x => types.contains(x.type_id).ToList();
Run Code Online (Sandbox Code Playgroud)
(type_id不是主键).
谢谢,
标记
使用EditorTemplate时,如果我希望模型上的值显示在屏幕上,还要发布回控制器,我应该使用什么帮助器?
即.如果我使用TextBoxFor:
@Html.TextBoxFor(model => model.RoomTypeName)
Run Code Online (Sandbox Code Playgroud)
...然后用户可以修改文本...
我宁愿只显示文字,但如果我使用:
@Html.DisplayTextFor(model => model.RoomTypeName)
Run Code Online (Sandbox Code Playgroud)
...然后不会回发给控制器.
因此,我可以显示文本的唯一方法,也是为了确保我的模型状态有效,添加第二个隐藏字段,例如:
@Html.DisplayTextFor(model => model.RoomTypeName)
@Html.HiddenFor(model => model.RoomTypeName)
Run Code Online (Sandbox Code Playgroud)
我知道这有效,但我想知道是否有更优雅的方式 - 所以我可以显示值,并将其发回,而不需要将其复制为隐藏元素?
谢谢,
标记
我刚刚下载了,并在我的W7笔记本电脑上安装了VS Express RC 2013.
http://www.microsoft.com/visualstudio/eng/2013-downloads#d-2013-express
我点击了Create New Project,并接受了所有默认值:
但是,在按下上面的"创建项目"按钮后,我收到以下消息:
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
我重新启动了笔记本电脑等 - 但仍然收到了这条消息 - 但它没有给我任何东西继续,我找不到类似的问题发布在SO /谷歌等.
任何人都可以对此有所了解,还是建议修复它?
更新:我尝试了2012版的此修复程序,但错误仍然发生:http://forums.asp.net/t/1887489.aspx
谢谢,
标记
是否有可能在Razor中使用"Sum",因此您可以总结视图中已经完成的内容.即.我的观点是这样的:
@model IEnumerable<cb2.ViewModels.ResultsVM>
...
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Qualified)
</td>
...
}
Run Code Online (Sandbox Code Playgroud)
然后我想总结屏幕底部的所有Qualified in,类似于:
@Model.Qualified.Sum()
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
'System.Collections.Generic.IEnumerable<cb2.ViewModels.ResultsVM>' does not contain a definition for 'Qualified'
我认为Qazor在模型上简单地使用Sum或Count会很容易吗?
谢谢,马克
我正在使用JQM-DateBox - 它需要以下Razor标记:
@Html.TextBoxFor(m => m.From, new { @name = "mydate", @id = "mydate",
@data_role = "datebox",
@data_options = "{'mode':'flipbox', 'dateFormat':'dd/mm/YYYY' ,'useNewStyle':true} ")
Run Code Online (Sandbox Code Playgroud)
但是,这呈现为:
<input data-options=" {'mode':'flipbox', 'dateFormat':'dd/mm/YYYY' ,'useNewStyle':true} "
data-role="datebox" id="mydate"
name="From" type="text" value="29/08/2013 00:00:00" />
Run Code Online (Sandbox Code Playgroud)
我知道你可以使用html.raw - 但你如何在帮助器中使用它?
我需要表明:
{'mode':
...代替...
{'mode':
我正在使用Twitter Bootstrap和ASP.Net MVC.
如何右键对齐TextBoxFor中的文本:
@Html.TextBoxFor(model => model.PaymentReceived,
new { @class = "pmtallocated pull-right" , .dir = "rtl" })
Run Code Online (Sandbox Code Playgroud)
我Invalid expression term '.'
在上面尝试时遇到错误.
谢谢,
标记
我正在使用Visual Studio Express 2013.
在尝试安装nuget软件包时,我收到错误(在2012年快递中尝试时没有错误):
PM> Install-Package AutoMapper
Install-Package : Unable to connect to the remote server
At line:1 char:16
+ Install-Package <<<< AutoMapper
+ CategoryInfo : NotSpecified: (:) [Install-Package], WebException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
Run Code Online (Sandbox Code Playgroud)
互联网正在运行(我有Vs 2012工作和同时安装nuget包 - 没有问题).
VS - >工具 - >扩展和更新说:Cannot check for updates because the following error(s) occurred whil trying to contact the server: Unable to connect to the remote server - please click here to retry the request
- 当我点击它时,它会回到"检索信息"循环.
有没有人有任何想法如何解决这个问题?
谢谢,马克
有谁知道CreditCardAttribute
ASP.Net MVC 4上的Class 是否会阻止开关卡被验证?
在我的视图模型中,我将它设置为:
[CreditCard]
[Required]
[Display(Name = "Card Number")]
public string CardNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
我已经使用Visa和Mastercard进行了测试,但是当进入Switch卡时,它不允许通过.