你能否将MVC指向默认文件夹以外的文件夹(Views/Shared/EditorTemplates&Views/Shared/DisplayTemplates)?我想将它们放在子文件夹下面的子文件夹中,或者放在Shared文件夹之外的其他文件夹中.
例如,如果我在此文件夹下有一个编辑器模板:
〜\查看\订单\ ProductModel.cshtml
如何告诉我的EditorFor模板使用这个tempalte名称?
我试过完全限定它,但这不起作用:
@Html.EditorFor(m => m.ProductModel, @"~\Views\Order\ProductModel.cshtml")
Run Code Online (Sandbox Code Playgroud)
我尝试过使用正斜杠和反斜杠,有/无.chstml,我能想到的每一个组合.我开始认为这不受支持,但我无法想象为什么它不会.
我正在开发我的第一个ASP.NET MVC 3应用程序,我有一个看起来像这样的视图:
@model IceCream.ViewModels.Note.NotesViewModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(m => m.Name)
foreach (var item in Model.Notes)
{
@Html.EditorFor(m => item);
}
<input type="submit" value="Submit"/>
}
Run Code Online (Sandbox Code Playgroud)
我有一个如下所示的EditorTemplate:
@model IceCream.ViewModels.Note.NoteViewModel
<div>
@Html.HiddenFor(m => m.NoteID)
@Html.TextBoxFor(m => m.NoteText)
@Html.CheckBoxFor(m => m.IsChecked)
</div>
Run Code Online (Sandbox Code Playgroud)
NotesViewModel看起来像这样:
public class NotesViewModel
{
public string Name { get; set; }
public IEnumerable<NoteViewModel> Notes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
NoteViewModel如下所示:
public class NoteViewModel
{
public int NoteID { get; set; }
public System.DateTime Timestamp { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用EditorTemplate在父视图的表中显示子集合.我遇到的问题是,如果模板的名称与子类的名称完全相同,这似乎只能起作用.当我尝试使用名称略有不同的模板,并将该名称作为templateName参数传递给EditorFor时,我收到运行时错误.我希望我可以使用相同的子集合将不同的子EditorTemplates用于不同的目的.这是一个简短的例子:
楷模:
public class Customer
{
int id { get; set; }
public string name { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int id { get; set; }
public DateTime orderdate { get; set; }
public decimal amount { get; set; }
public Customer customer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
客户控制器Index()方法:
public ActionResult Index()
{
Customer customer = new Customer() {id = 1, name = "Acme Corp.", Orders = new …Run Code Online (Sandbox Code Playgroud)