C# .NET Core:未呈现ViewModel属性
我正在使用Razor视图引擎开发一个ASP.NET Core 2.0 web应用程序。首先也是最重要的是,我试图通过互联网和StackOverFlow在这里寻找解决方案,但任何这些都可以解决我的问题。让我们看看我们面临的是什么: 我为客户提供了一个相对简单的视图模型:C# .NET Core:未呈现ViewModel属性,c#,razor,asp.net-core,C#,Razor,Asp.net Core,我正在使用Razor视图引擎开发一个ASP.NET Core 2.0 web应用程序。首先也是最重要的是,我试图通过互联网和StackOverFlow在这里寻找解决方案,但任何这些都可以解决我的问题。让我们看看我们面临的是什么: 我为客户提供了一个相对简单的视图模型: namespace MokaKukaMap.Application.Customers.ViewModels.Models { public class CustomerViewModel { pu
namespace MokaKukaMap.Application.Customers.ViewModels.Models
{
public class CustomerViewModel
{
public long Id { get; set; }
[Display(Name = "Név")]
[Required]
public string Name { get; set; }
[Display(Name = "Telefonszám")]
[Required]
public string PhoneNumber { get; set; }
}
}
如您所见,Name
和PhoneNumber
属性指定了属性Display
。
我对应的部分视图(\u NewCustomer.cshtml
)如下所示:
@model MokaKukaMap.Application.Customers.ViewModels.Models.CustomerViewModel
<div class="col-lg-5 basic-surronder">
<form asp-controller="NewCustomer" asp-action="NewCustomer" class="well">
<div id="form-horizontal">
<div class="form-group">
<label asp-for="@Model.Name" class=""></label>
<div class="">
<input asp-for="@Model.Name" class="form-control">
<span asp-validation-for="@Model.Name" class="text-danger"> </span>
</div>
</div>
<div class="form-group">
<label asp-for="@Model.PhoneNumber" class=""></label>
<div class="">
<input asp-for="@Model.PhoneNumber" class="form-control">
<span asp-validation-for="@Model.PhoneNumber" class="text-danger"> </span>
</div>
</div>
</div>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<div class="">
<input type="submit" formnovalidate="formnovalidate" value="Új ügyfél hozzáadása" class="btn btn-primary" />
</div>
</div>
</form>
</div>
\u NewCustomer.cshtml
位于标准视图/共享文件夹中。我想做的是将这个局部视图放到一个特定的文件夹中,即Customers/Views文件夹。为了解决这个问题,我制作了一个CustomerServiceWLocationExpander
,如下所示:
public class CustomViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
var viewLocationFormats = new[]
{
"~/Customers/Views/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/{0}.cshtml",
};
return viewLocationFormats;
}
public void PopulateValues(ViewLocationExpanderContext context)
{
context.Values["customviewlocation"] = nameof(CustomViewLocationExpander);
}
}
@Html.Partial("~/Customers/Views/_NewCustomer.cshtml")
此外,我还意识到,如果部分视图位于自定义文件夹中,那么VS2017(IntelliSense)无法识别asp net核心特定的html标记,例如
不确定这是否有助于解决实际问题,因为您可能需要更改很多内容。。但我发现这个结构比你那里的更“干净”。我立即看到的问题是:
- 难以区分实际功能和设置/红外线代码。
Containers
是Docker的一项功能还是与之相关?不知道,我必须打开它
- 现在的情况是,在您的
IViewLocationExpander
中,您必须对每个功能名称进行“硬编码”,因为它们都分布在周围。。。不太酷
我的建议是将结构改为:
- 功能(将视图重命名为功能,正常保留共享文件夹。)
- 顾客
- 控制器
- 观点
然后,将ViewLocationExpander
配置如下:
public class ViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// {1} is Controller, {0} is View Name
return new []
{
"/Features/{1}/Views/{0}.cshtml", // Finds the Customer View folders
"/Features/{1}/{0}.cshtml", // finds the Home views.. they are in root of the folder.. so no need for Views
"/Features/Shared/{0}.cshtml" // Layouts
};
}
public void PopulateValues(ViewLocationExpanderContext context)
{
}
}
公共类ViewLocationExpander:IViewLocationExpander
{
公共IEnumerable ExpandViewLocations(ViewLocationExpanderContext上下文,IEnumerable viewLocations)
{
//{1}是控制器,{0}是视图名
返回新的[]
{
“/Features/{1}/Views/{0}.cshtml”,//查找客户视图文件夹
“/Features/{1}/{0}.cshtml”,//查找主视图..它们位于文件夹的根目录中..因此不需要视图
“/Features/Shared/{0}.cshtml”//Layouts
};
}
public void PopulateValues(ViewLocationExpanderContext上下文)
{
}
}
通过这种方式,您可以使用它自己的视图
文件夹添加新功能,例如产品
,并将自动拾取。以下是我在设置中使用的结构的图像:
编辑:这是我在Github上的解决方案
此外,我并不真正理解在单独的项目中使用ViewModels的意义。ViewModels的全部目的是为UI服务。ViewModels上没有DDD,带有公共setter。也许我误解了你。。但我肯定会更深入地研究这个问题。DDD是丰富的,通常是不变的实体。。而ViewModels只是用于传输数据的poco类。您能发布您的文件夹结构吗?我可以毫无问题地完成它,但我使用了不同的文件夹结构。。不确定是否可以更改结构。另外,您是否在ConfigureServices
方法中配置了CustomViewLocationExpander
?@jpgrassi感谢您的评论!CustomViewLocationExpander
在服务中配置。至于文件夹结构,我添加了它的一个图像。是的,我可以灵活地更改文件夹结构,但我想将部分视图放入问题中描述的自定义文件夹(客户/视图)。还是你有更好的主意?我明白了。。我所做的是将视图文件夹“重命名”为类似“功能”的内容。将尝试在你的结构中重现,看看它是否也有效。检查我的答案。。它对我有用,但有点超出了你当前的设置。如果你想试试的话,我可以把它放在github上。请告诉我。
services.Configure<RazorViewEngineOptions>(o => o.ViewLocationFormats.Add("/Customers/Views/{0}" + RazorViewEngine.ViewExtension));
@Html.Partial("~/Customers/Views/_NewCustomer.cshtml")
public class ViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// {1} is Controller, {0} is View Name
return new []
{
"/Features/{1}/Views/{0}.cshtml", // Finds the Customer View folders
"/Features/{1}/{0}.cshtml", // finds the Home views.. they are in root of the folder.. so no need for Views
"/Features/Shared/{0}.cshtml" // Layouts
};
}
public void PopulateValues(ViewLocationExpanderContext context)
{
}
}