将 HtmlHelper 用于内部模型属性
Using HtmlHelper for inner Model properties
我的 Asp.net MVC 视图之一有这样一个模型结构:
public class MyModel{
public AnotherModel1 Prop1 {get;set;}
public AnotherModel2 Prop2 {get;set;}
}
public class AnotherModel1{
public InnerModel InnerProp1 {get;set;}
public InnerModel InnerProp2 {get;set;}
public InnerModel InnerProp3 {get;set;}
}
public class AnotherModel2{
public InnerModel InnerProp1 {get;set;}
public InnerModel InnerProp2 {get;set;}
}
public class InnerModel {
public string Input {get;set;}
}
这意味着,MyModel 是传递给View 的通用模型。其他一些模型 AnotherModel1、AnotherModel2 包含 InnerModel 作为属性。
我想创建一个知道如何呈现 InnerModel 的辅助函数。
然后在我的页面视图中,我希望能够编写类似
@RenderInnerModel(m => m.Prop1.InnerProp1)
问题是,我不知道如何将 htmlhelper 传递到我的函数中,以便能够在我的助手中使用 @Html.TextBoxFor(m => m.Input)。
虽然这个问题可以通过使用每个 InnerModel 的部分视图来解决,但我想要参数化的辅助函数并且不想搞乱复杂的部分模型,例如使用元组等
编辑
虽然 EditorFor 似乎几乎是我想要的,但我仍然想了解是否可以有类似的东西
var modelHelper = new HtmlHelper<InnerModel>(ViewContext, this);
但是在 MyModel 的上下文中?谢谢!
您的控制器:
public class AnotherModel
{
public InnerModel InnerProp1 { get; set; }
public InnerModel InnerProp2 { get; set; }
}
public class InnerModel
{
public string Input { get; set; }
}
public class EditorExampleController : Controller
{
//
// GET: /EditorExample/
public ActionResult Index()
{
AnotherModel model = new AnotherModel();
model.InnerProp1 = new InnerModel { Input = "test 1" };
model.InnerProp2 = new InnerModel { Input = "test 2" };
return View(model);
}
}
您的看法:
@model MVCApp.Controllers.AnotherModel
<h2>Editor template</h2>
@Html.EditorFor(_ => _.InnerProp1)
@Html.EditorFor(_ => _.InnerProp2)
在 "Views" 中的 "shared" 文件夹上创建一个新文件夹调用 "EditorTemplates",然后添加局部视图 "InnerModel.cshtml" :
@model MVCApp.Controllers.InnerModel
@if (Model.Input == "test 2")
{
<h4>@Model.Input</h4>
}
else
{
<h2>@Model.Input</h2>
}
这是一个简单的实现,如果你需要,我可以post一个自定义的 HtmlHelper
我的 Asp.net MVC 视图之一有这样一个模型结构:
public class MyModel{
public AnotherModel1 Prop1 {get;set;}
public AnotherModel2 Prop2 {get;set;}
}
public class AnotherModel1{
public InnerModel InnerProp1 {get;set;}
public InnerModel InnerProp2 {get;set;}
public InnerModel InnerProp3 {get;set;}
}
public class AnotherModel2{
public InnerModel InnerProp1 {get;set;}
public InnerModel InnerProp2 {get;set;}
}
public class InnerModel {
public string Input {get;set;}
}
这意味着,MyModel 是传递给View 的通用模型。其他一些模型 AnotherModel1、AnotherModel2 包含 InnerModel 作为属性。
我想创建一个知道如何呈现 InnerModel 的辅助函数。 然后在我的页面视图中,我希望能够编写类似 @RenderInnerModel(m => m.Prop1.InnerProp1)
问题是,我不知道如何将 htmlhelper 传递到我的函数中,以便能够在我的助手中使用 @Html.TextBoxFor(m => m.Input)。
虽然这个问题可以通过使用每个 InnerModel 的部分视图来解决,但我想要参数化的辅助函数并且不想搞乱复杂的部分模型,例如使用元组等
编辑
虽然 EditorFor 似乎几乎是我想要的,但我仍然想了解是否可以有类似的东西
var modelHelper = new HtmlHelper<InnerModel>(ViewContext, this);
但是在 MyModel 的上下文中?谢谢!
您的控制器:
public class AnotherModel
{
public InnerModel InnerProp1 { get; set; }
public InnerModel InnerProp2 { get; set; }
}
public class InnerModel
{
public string Input { get; set; }
}
public class EditorExampleController : Controller
{
//
// GET: /EditorExample/
public ActionResult Index()
{
AnotherModel model = new AnotherModel();
model.InnerProp1 = new InnerModel { Input = "test 1" };
model.InnerProp2 = new InnerModel { Input = "test 2" };
return View(model);
}
}
您的看法:
@model MVCApp.Controllers.AnotherModel
<h2>Editor template</h2>
@Html.EditorFor(_ => _.InnerProp1)
@Html.EditorFor(_ => _.InnerProp2)
在 "Views" 中的 "shared" 文件夹上创建一个新文件夹调用 "EditorTemplates",然后添加局部视图 "InnerModel.cshtml" :
@model MVCApp.Controllers.InnerModel
@if (Model.Input == "test 2")
{
<h4>@Model.Input</h4>
}
else
{
<h2>@Model.Input</h2>
}
这是一个简单的实现,如果你需要,我可以post一个自定义的 HtmlHelper