我在我的模型上有一组对象,我使用EditFor函数在View中渲染,我有一个EditorTemplate,负责实际渲染每个对象.
@Html.EditorFor(model => model.MyObjects)
Run Code Online (Sandbox Code Playgroud)
这已经运行了一段时间了,当你检查html时,我的文本框以model属性为前缀,后面跟着它们来自的集合的索引.
<input class="text-box single-line" id="MyObjects_2__SomeProperty"
name="MyObjects[2].SomeProperty" type="Text" value="" />
Run Code Online (Sandbox Code Playgroud)
但是我最近开始在集合的模型元数据中使用ShowForEdit和ShowForDisplay属性,如果ShowForEdit不是真的,在我的编辑器模板的第一行,我只是跳过它.
@if (!ViewData.ModelMetadata.ShowForEdit)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
但由于这些都是在html中编入索引,当我尝试通过回发将此集合保存回viewmodel时,由于依赖于索引编号而失败.当我检查它的值时,我的视图模型中缺少缺少索引后的集合中的每个项目.
在这种情况下,它实际上是我正在跳过的集合中的第一个项目,因为我不希望它在编辑视图中可见,但是因为当我回发html中的第一个索引是1(而不是0)像往常一样),但是当你尝试保存更改时这是一个问题.使用javascript更改DOM时,这也是一个问题.
当html表示集合中的一个或多个索引不存在时,有没有其他人遇到默认模型绑定器读取回发数据的能力?
是否有模型粘合剂来处理这个问题?
asp.net-mvc defaultmodelbinder model-binding modelbinders asp.net-mvc-3
我有一个MVC应用程序,它使用从父对象类型继承的动态业务对象.例如,基类Client可能有两个子类调用Vendor和ServiceProvider,而这些都是由同一个控制器来处理.我有一个部分视图,我在查看客户端的详细信息时加载到页面的右侧_Aside.cshtml.当我加载客户端时,我首先尝试寻找一个特定的Aside,然后我加载了一个通用的.下面是代码的样子.
@try
{
@Html.Partial("_" + Model.Type.TypeName + "Aside")
}
catch (InvalidOperationException ex)
{
@Html.Partial("_Aside")
}
Run Code Online (Sandbox Code Playgroud)
TypeName属性中包含"Vendor"或"ServiceProvider".
现在这个工作正常,但问题是我只希望它在未找到视图时进行故障转移,当InvalidOperationException部分视图实际抛出时(通常是它可能调用的子操作的结果),它也会失败.我想过要检查,Exception.Message但这看起来有点hackish.有没有其他方法我可以得到所需的结果,而无需检查Message属性或这是我唯一的选择吗?
ex.Message = "The partial view '_ServiceProviderAside' was not found or no view
engine supports the searched locations. The following locations were
searched: (... etc)"
Run Code Online (Sandbox Code Playgroud)
更新:这是根据杰克的回答,我目前在我的项目中使用扩展方法的类,以及Chao的建议.
//For ASP.NET MVC
public static class ViewExtensionMethods
{
public static bool PartialExists(this HtmlHelper helper, string viewName)
{
if (string.IsNullOrEmpty(viewName)) throw new ArgumentNullException(viewName, "View …Run Code Online (Sandbox Code Playgroud) 我努力为此寻找解决方案好几天,所以我想分享我的情况.我正在将现有的ASP.NET MVC应用程序转换为ASP.NET Core MVC.然而,使用ASP.NET Core的最大变化是System.Web命名空间是不行的.但是,通常,SQL Server Reporting Services(SSRS)通常作为WebReference添加到项目中,该项目基于 - 您猜对了System.Web.
因此,我需要找到一种能够命中SSRS端点以执行报告的替代方法.对于我的场景,我主要想要PDF(尽管调用SSRS的Render方法可以让你选择导出格式).
这个问题的解决方案提出了它自己的问题,最明显的错误是:
Microsoft.ReportingServices.Diagnostics.Utilities.MissingSessionIdException:缺少会话标识符.此操作需要会话标识符.
所以我最后回答的两个问题可能对其他人有价值,如何在没有System.Web的情况下使用SSRS,以及如何解决"缺少会话标识符"的错误
我有一个select语句,它组合了一个人名的多个段.这不是什么新鲜事.
SELECT FirstName + ' ' + LastName AS FullName FROM MyTable
Run Code Online (Sandbox Code Playgroud)
然后我尝试将中间的首字母添加到此,我想出了以下内容
SELECT FirstName + ' ' + ISNULL(MiddingInitial + ' ', '') + LastName AS FullName FROM MyTable
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但在我测试期间ISNULL(),我遇到了一个奇怪的行为.我知道NULL + 'any string'解决了NULL.然而,这简直奇怪......
这是我的代码,以及我得到的结果......
print '''' + isnull(null + 'Any String','Results in null') + ''''
print '''' + isnull(null + 'Any','Results in null') + ''''
print '''' + isnull(null + 'A','Results in null') + ''''
print '''' + isnull(null + '','Results in null') …Run Code Online (Sandbox Code Playgroud) 我已将我的项目升级到 asp.net 核心。但是现在我的 CookieAuthnetication 和 OpenIdConnectionAuthentication 方法不起作用。它们已经过时了。
Startup.cs 配置方法
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ClientId = "integapay.client",
ClientSecret = "mySecret",
ResponseType = "code id_token",
Scope = { "openid", "profile", "api.public", "offline_access" },
GetClaimsFromUserInfoEndpoint = true,
SaveTokens = true
});
Run Code Online (Sandbox Code Playgroud) 我搜索网络寻找一种方法将"subItem1"和"subItem2"添加到"Home"项目菜单(在示例代码中),但我发现使用java或复杂的razor示例.是否有更简单的方法只使用剃须刀?
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</nav>
Run Code Online (Sandbox Code Playgroud) 如何在 中添加多个 cookie 方案aspnet core 2.0?
我已按照此处的Auth 2.0 迁移公告 和此处的将身份验证和身份迁移到 ASP.NET Core 2.0 中的说明进行操作, 但我无法添加多个方案。
例如:
services.AddAuthentication("myscheme1").AddCookie(o =>{
o.ExpireTimeSpan = TimeSpan.FromHours(1);
o.LoginPath = new PathString("/forUser");
o.Cookie.Name = "token1";
o.SlidingExpiration = true;
});
services.AddAuthentication("myscheme2").AddCookie(o =>{
o.ExpireTimeSpan = TimeSpan.FromHours(1);
o.LoginPath = new PathString("/forAdmin");
o.Cookie.Name = "token2";
o.SlidingExpiration = true;
});
Run Code Online (Sandbox Code Playgroud) 好的,我之前的问题/设置有太多的变量,所以我将其分解为简单的骨骼组件。
鉴于以下使用 StructureMap3 的代码...
//IoC setup
For<HttpContextBase>().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null ));
For<ICurrentUser>().Use<CurrentUser>();
//Classes used
public class CurrentUser : ICurrentUser
{
public CurrentUser(HttpContextBase httpContext)
{
if (httpContext == null) return;
if (httpContext.User == null) return;
var user = httpContext.User;
if (!user.Identity.IsAuthenticated) return;
UserId = httpContext.User.GetIdentityId().GetValueOrDefault();
UserName = httpContext.User.Identity.Name;
}
public Guid UserId { get; set; }
public string UserName { get; set; }
}
public static class ClaimsExtensionMethods
public static Guid? GetIdentityId(this IPrincipal …Run Code Online (Sandbox Code Playgroud) c# structuremap asp.net-mvc dependency-injection structuremap3
asp.net-core ×3
asp.net-mvc ×3
c# ×2
razor ×2
.net ×1
asp.net ×1
cookies ×1
html5 ×1
modelbinders ×1
sql ×1
sql-server ×1
structuremap ×1
viewengine ×1
wcf ×1