快速刷新.
什么时候:
IEnumerable<T>Html.EditorFor()使用仅接受lambda表达式的重载T在Views/Shared/EditorTemplates下有一个类型的编辑器模板然后MVC引擎将自动为可枚举序列中的每个项调用编辑器模板,生成结果列表.
例如,当存在Order具有属性的模型类时Lines:
public class Order
{
public IEnumerable<OrderLine> Lines { get; set; }
}
public class OrderLine
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并且有一个视图Views/Shared/EditorTemplates/OrderLine.cshtml:
@model TestEditorFor.Models.OrderLine
@Html.EditorFor(m => m.Prop1)
@Html.EditorFor(m => m.Prop2)
Run Code Online (Sandbox Code Playgroud)
然后,当您@Html.EditorFor(m => m.Lines)从顶级视图调用时,您将获得一个页面,其中包含每个订单行的文本框,而不仅仅是一个.
但是,正如您在链接问题中看到的那样,这仅在您使用特定的重载时才有效EditorFor.如果您提供模板名称(为了使用未在OrderLine类之后命名的模板),则不会发生自动序列处理,而是会发生运行时错误.
此时,您必须将自定义模板的模型声明为IEnumebrable<OrderLine>并以某种方式手动迭代其项目以输出所有这些项目,例如
@foreach (var line in Model.Lines) {
@Html.EditorFor(m …Run Code Online (Sandbox Code Playgroud) 我无法找到Razor 3中的新功能.这似乎是一个愚蠢的问题,但我可以很容易地找到MVC 5中的新功能,在EF 6中等等 - 但我试着谷歌它,我试过asp.net,我试过斯科特的博客 - 什么都没有.所以我很好奇,有没有人真正知道Razor 3中的新功能?谢谢!
我想创建一个可用的View类库,包括使用Razor的视图.在我的观点中,我很难实现Razor Intellisense.
我已经做了很多研究来寻找解决方案,包括这个博客:http: //blogs.msdn.com/b/webdev/archive/2011/01/20/how-to-get-razor-intellisense-for -model-in-a-class-library-project.aspx和这个相关的帖子:类库中的Razor,缺少intellisense,但到目前为止没有任何工作.
我尝试的最后一件事是在我的View类库中添加了一个web.config文件,但这并没有启用Razor Intellisense.
web.config中:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" …Run Code Online (Sandbox Code Playgroud) 请参阅此类似问题:需要在User.Identity中访问更多用户属性
我想创建自定义身份验证方法以与我的Razor视图一起使用,它允许轻松访问IdentityUser与User.Identity对象关系的属性,但我不确定如何去做.我想创建一个类似于几个自定义扩展User.Identity.GetUserName(),User.Identity.GetUserById()等等...而不是使用此ViewContextExtension方法.我的身份验证类型当前是DefaultAuthenticationTypes.ApplicationCookieVS2013 MVC5模板的默认类型.正如Shoe所说,我需要在用户登录后插入此声明.
我的问题是:
如何以及在何处创建具有this IIdentityIPrincipal下的out参数的自定义声明?
这将允许我通过CookieAuthentication在DDD设置中的实体的View中访问用户属性,其中我在使用Identity 2.0的单个应用程序中有多个DbContexts.我最终将使用WebAPI,但是现在我希望尽可能简单.我找到了这个SO Q&A但是它适用于使用Tickets的Web Forms.不确定门票和代币之间的区别吗?
这是ViewContext从基本控制器使用的当前方法:
视图:
@using Microsoft.AspNet.Identity
@using Globals.Helpers
@using Identity //custom Identity for Domain
@using Microsoft.AspNet.Identity.Owin
@if (Request.IsAuthenticated)
{
var url = @ViewContext.BaseController().GetAvatarUrlById(User.Identity.GetUserId<int>());
//...
}
Run Code Online (Sandbox Code Playgroud)
BaseController.cs
public string GetAvatarUrlById(int id)
{
var user = UserManager.FindById(id);
return "../../" + user.ImageUrl;
}
Run Code Online (Sandbox Code Playgroud)
Extensions.cs
public static class ViewContextExtension
{
public static BaseController BaseController(this ViewContext view)
{
var baseController …Run Code Online (Sandbox Code Playgroud) c# class-extensions asp.net-mvc-5 razor-3 asp.net-identity-2
我有一段简单的代码,试图填充3个国家/地区的列表,并默认选择第2项.由于某种原因,@ Html.DropDownList没有初始化屏幕中的第二项.有人能指出这段代码有什么问题吗?
public class DemoController : Controller
{
[HttpGet]
public ActionResult Test()
{
List<SelectListItem> ddlCountries = new List<SelectListItem>();
SelectListItem item1 = new SelectListItem { Selected = false, Text = "USA", Value = "1" };
SelectListItem item2 = new SelectListItem { Selected = true, Text = "Ireland", Value = "2" };
SelectListItem item3 = new SelectListItem { Selected = false, Text = "UK", Value = "3" };
ddlCountries.Add(item1);
ddlCountries.Add(item2);
ddlCountries.Add(item3);
ViewBag.ddlCountries = ddlCountries;
return View();
}
[HttpPost]
public string Test(string ddlCountries) …Run Code Online (Sandbox Code Playgroud)