我有一种方法是调用 api 方法。该 api 方法包含插入、更新、删除的 SQL 语句。但是当存储过程抛出任何错误时,如何在前面显示为错误消息。我正在使用 ASP .NET 5 和 MVC 6。我的方法如下:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Method(Model model)
{
string url = ConfigurationSettingHelper.BaseUrl + "apiurl";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsJsonAsync<Model>(url, model);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
var Msg = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(data);
if (!string.IsNullOrEmpty(Convert.ToString(Msg)))
{
//Here code to display error message.
}
}
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
帮助我在页面上显示 Msg 变量字符串消息。
谢谢你
我想在 MVC-6 RC2 中使用自定义 jquery 不显眼的验证器
与旧答案类似,我在This one 中看到了一些 RC2 示例,但我不知道如何为文件实现它。
这是我的查看模式
public class FileUploadViewModel
{
//TODO [FileType(Validtype="jpeg,png,jif", MaxSize=112222)]// this is what I want
[Required(ErrorMessage = "Please select a file")]
public IFormFile File { get; set; }
[Required(ErrorMessage = "Please select link")]
public string FileName { get; set; }
public string ExternalLink { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud) asp.net-mvc unobtrusive-validation asp.net-core-mvc .net-core-rc2
根据内联文档,ControllerBase.RedirectToAction获取操作名称和控制器名称:
// Parameters:
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName);
Run Code Online (Sandbox Code Playgroud)
现在,让我们假设我想重定向到以下操作:
[Route("Whatever")]
public class WhateverController : Controller
{
[HttpGet("Overview")]
public IActionResult Overview()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
当然,我想使用nameof运算符":
[Route("Home")]
public class HomeController : Controller
{
[HttpGet("Something")]
public IActionResult Something()
{
return RedirectToAction(
nameof(WhateverController.Overview), // action name
nameof(WhateverController) // controller name
);
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个调用因错误而失败 InvalidOperationException: No route matches the …
我正在尝试渲染一个链接列表,图标应该根据是否找到项目ID而改变IEnumerable.
到目前为止,这是我观点的相关部分:
@{
if (product.InFrontPages.Contains(item.ParentCategory.Id))
{
<span class="glyphicon glyphicon-checked"></span>
}
else
{
<span class="glyphicon glyphicon-unchecked"></span>
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致编译时错误:
'IEnumerable'不包含'Contains'的定义,并且最好的扩展方法重载'ParallelEnumerable.Contains(ParallelQuery,int)'需要一个'ParallelQuery'类型的接收器
我想我可能想要实现这个问题的公认答案,但我还没想出怎么做.当他建议实现通用接口时,我不明白Jon的意思.
涉及的视图模型:
public class ViewModelProduct
{
public int Id { get; set; }
public string Title { get; set; }
public string Info { get; set; }
public decimal Price { get; set; }
public int SortOrder { get; set; }
public IEnumerable<FrontPageProduct> InFrontPages { get; set; }
public IEnumerable<ViewModelCategoryWithTitle> Categories { get; set; }
}
public …Run Code Online (Sandbox Code Playgroud) 我找不到任何关于此的细节,但我遇到的问题是,如果Validate调用在任何子属性上失败,则不会调用父对象的Validate函数.简单场景如下:
public class Child : IValidateObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{ ... }
}
public class Parent : IValidatableObject
{
public Child Child { get; set;}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{ ... }
}
Run Code Online (Sandbox Code Playgroud)
如果子级中的验证失败,则父级的Validate函数不会被调用,因此您最终必须首先解决所有子问题然后提交,然后才会看到父级的所有验证失败.
如果有人可以帮助我理解为什么会这样,或者指出一些有关这方面的文件会很棒.
我的ASP.NET Core Web项目具有通常的共享布局文件。
如何排除特定页面使用的布局?
razor asp.net-core-mvc asp.net-core asp.net-core-2.0 razor-pages
我试图从下拉列表中清除所选值,但值仍然是持久的.这是使用相同的行为@Html.DropDownListFor
调节器
public class HomeController : Controller
{
[Route("/Home/Index")]
[Route("/Home/Index/{Category}")]
[Route("/Home/Index/{Category}/{Type}")]
public IActionResult Index(HomeModel model)
{
// Issue is here
// for url: home/index/accessories/test
// "Category" is cleared if it is not valid "type"
// but still "Accessories" remains selected in the drop down
if (model.Type != "Electronics" && model.Type != "Furniture")
{
model.Category = string.Empty;
}
return View(new HomeModel() { Category = model.Category, Type = model.Type });
}
Run Code Online (Sandbox Code Playgroud)
视图
@model WebApplication1.Controllers.HomeModel
<select asp-for="Category" asp-items="@Model.Categories"></select>
<select asp-for="Type" asp-items="@Model.Types"></select>
Run Code Online (Sandbox Code Playgroud)
模型 …
假设我想编写自己的Login,Logout端点及其Views
但出于某种原因,我正努力去除现有的端点
每当我删除可能与这些端点关联的内容时,它们都会重新创建并返回其默认视图.
基本上我想删除从ASP.NET Core Identity生成的那些默认端点/视图
关于如何实现这一点的任何想法?
"Templates/Identity/Pages/Account/Account.AccessDenied.cs.cshtml",
"Templates/Identity/Pages/Account/Account.AccessDenied.cshtml",
"Templates/Identity/Pages/Account/Account.ConfirmEmail.cs.cshtml",
"Templates/Identity/Pages/Account/Account.ConfirmEmail.cshtml",
"Templates/Identity/Pages/Account/Account.ExternalLogin.cs.cshtml",
"Templates/Identity/Pages/Account/Account.ExternalLogin.cshtml",
"Templates/Identity/Pages/Account/Account.ForgotPassword.cs.cshtml",
"Templates/Identity/Pages/Account/Account.ForgotPassword.cshtml",
"Templates/Identity/Pages/Account/Account.ForgotPasswordConfirmation.cs.cshtml",
"Templates/Identity/Pages/Account/Account.ForgotPasswordConfirmation.cshtml",
"Templates/Identity/Pages/Account/Account.Lockout.cs.cshtml",
"Templates/Identity/Pages/Account/Account.Lockout.cshtml",
"Templates/Identity/Pages/Account/Account.Login.cs.cshtml",
"Templates/Identity/Pages/Account/Account.Login.cshtml",
"Templates/Identity/Pages/Account/Account.LoginWith2fa.cs.cshtml",
"Templates/Identity/Pages/Account/Account.LoginWith2fa.cshtml",
"Templates/Identity/Pages/Account/Account.LoginWithRecoveryCode.cs.cshtml",
"Templates/Identity/Pages/Account/Account.LoginWithRecoveryCode.cshtml",
"Templates/Identity/Pages/Account/Account.Logout.cs.cshtml",
"Templates/Identity/Pages/Account/Account.Logout.cshtml",
"Templates/Identity/Pages/Account/Account.Register.cs.cshtml",
"Templates/Identity/Pages/Account/Account.Register.cshtml",
"Templates/Identity/Pages/Account/Account.ResetPassword.cs.cshtml",
"Templates/Identity/Pages/Account/Account.ResetPassword.cshtml",
"Templates/Identity/Pages/Account/Account.ResetPasswordConfirmation.cs.cshtml",
"Templates/Identity/Pages/Account/Account.ResetPasswordConfirmation.cshtml",
"Templates/Identity/Pages/Account/Account._ViewImports.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ChangePassword.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ChangePassword.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.DeletePersonalData.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.DeletePersonalData.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.Disable2fa.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.Disable2fa.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.DownloadPersonalData.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.DownloadPersonalData.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.EnableAuthenticator.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.EnableAuthenticator.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ExternalLogins.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ExternalLogins.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.GenerateRecoveryCodes.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.GenerateRecoveryCodes.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.Index.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.Index.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ManageNavPages.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.PersonalData.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.PersonalData.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ResetAuthenticator.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ResetAuthenticator.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.SetPassword.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.SetPassword.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ShowRecoveryCodes.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.ShowRecoveryCodes.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.TwoFactorAuthentication.cs.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage.TwoFactorAuthentication.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage._Layout.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage._ManageNav.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage._StatusMessage.cshtml",
"Templates/Identity/Pages/Account/Manage/Account.Manage._ViewImports.cshtml",
(...)
Run Code Online (Sandbox Code Playgroud) 我的下拉列表有问题。它始终为空,即使在调试时,列表中有4个不同的条目也在asp-items中设置,如下所示:
我究竟做错了什么?
ViewModel:
public IEnumerable<SelectListItem> SelectRole { get; set; }
public string RoleId { get; set; }
Run Code Online (Sandbox Code Playgroud)
控制器:
model.SelectRole = _roleManager.Roles?.Select(s => new SelectListItem
{
Value = s.Id,
Text = s.Name
});
Run Code Online (Sandbox Code Playgroud)
视图:
<select asp-for="RoleId" asp-items="@Model.SelectRole" class="form-control" />
Run Code Online (Sandbox Code Playgroud) 在index.cshtml中,我使用锚标记助手作为
<a asp-action="Edit" asp-route-id="@Model.Id" asp-route-firstname="@Model.Name"</a>
Run Code Online (Sandbox Code Playgroud)
并在动作方法中
public IActionResult Edit(string id, string firstname)
{
// id and firstname are assigned correct values
// but RouteData.Values only has three entries which are: controller, action and id, where is firstname?
}
Run Code Online (Sandbox Code Playgroud)
但是我不能通过访问访问firstname值,RouteData.Values["firstname"];而可以通过访问id值RouteData.Values["id"];,为什么它对id有效但对其他自定义属性无效?
asp.net-core-mvc ×10
c# ×8
asp.net-core ×6
asp.net-mvc ×2
asp.net ×1
razor ×1
razor-pages ×1
tag-helpers ×1