Asp.net mvc 3 .NET编辑器模板中的多模型访问
我正在尝试为我的MVC web应用程序创建一个更“高级”的编辑器模板,但是我遇到了一点困难。在我解释之前,让我展示一下我的源代码 以下是我视图中的代码(使用模板): 以下是模板:Asp.net mvc 3 .NET编辑器模板中的多模型访问,asp.net-mvc-3,mvc-editor-templates,Asp.net Mvc 3,Mvc Editor Templates,我正在尝试为我的MVC web应用程序创建一个更“高级”的编辑器模板,但是我遇到了一点困难。在我解释之前,让我展示一下我的源代码 以下是我视图中的代码(使用模板): 以下是模板: @Html.EditorFor(model => model.Height, "UnitTemplate", new { unitModel = Model.HeightUnit, unitType = Units.Distance }) @{ Layout = null; } @using MyPro
@Html.EditorFor(model => model.Height, "UnitTemplate", new { unitModel = Model.HeightUnit, unitType = Units.Distance })
@{
Layout = null;
}
@using MyProject.Models;
@{
var unitModel = this.ViewData["unitModel"];
var unitType = this.ViewData["unitType"] as SelectList;
}
<div class="data-group">
<div class="editor-label">
@Html.LabelFor(model => model)
</div>
<div class="option1">
@Html.TextBoxFor(model => model)
</div>
<div class="units">@Html.DropDownListFor(model => unitModel, unitType, new { @class = "unit" })</div>
<div class="validation">
<div>@Html.ValidationMessageFor(model => model)</div>
</div>
</div>
@using MyProject.Models;
@model MyViewModel
@{
Layout = null;
}
<div class="data-group">
<div class="editor-label">
@Html.LabelFor(model => model.Height)
</div>
<div class="option1">
@Html.TextBoxFor(model => model.Height)
</div>
<div class="units">
@Html.DropDownListFor(
model => model.HeightUnit,
Units.Distance,
new { @class = "unit" }
)
</div>
<div class="validation">
<div>
@Html.ValidationMessageFor(model => model.Height)
</div>
</div>
</div>
@{
布局=空;
}
@使用MyProject.Models;
@{
var unitModel=this.ViewData[“unitModel”];
var unitType=this.ViewData[“unitType”]作为SelectList;
}
@LabelFor(model=>model)
@TextBoxFor(model=>model)
@DropDownListFor(model=>unitModel,unitType,new{@class=“unit”})
@Html.ValidationMessageFor(model=>model)
如您所见,我有一些值(在本例中为Height),并且我还有一个与该值关联的单位类型(HeightUnit)。我希望能够一般地传递与模型相关联的单位值(因为我在很多地方使用这个模板)以及单位类型(因为这也可以更改)。最终,目标是允许用户在值之间快速转换
幸运的是,到目前为止,一切都正常(转换、填充等),只是当我保存结果时,unitModel不会保存到数据库中。是否有人建议我如何使其工作?在这种情况下,您可能希望对整个模型使用编辑器模板,因为此模板取决于主视图模型的多个属性(
Height
和HeightUnit
):
然后在~/Views/Shared/EditorTemplates/UnitTemplate.cshtml
编辑器模板中:
@Html.EditorFor(model => model.Height, "UnitTemplate", new { unitModel = Model.HeightUnit, unitType = Units.Distance })
@{
Layout = null;
}
@using MyProject.Models;
@{
var unitModel = this.ViewData["unitModel"];
var unitType = this.ViewData["unitType"] as SelectList;
}
<div class="data-group">
<div class="editor-label">
@Html.LabelFor(model => model)
</div>
<div class="option1">
@Html.TextBoxFor(model => model)
</div>
<div class="units">@Html.DropDownListFor(model => unitModel, unitType, new { @class = "unit" })</div>
<div class="validation">
<div>@Html.ValidationMessageFor(model => model)</div>
</div>
</div>
@using MyProject.Models;
@model MyViewModel
@{
Layout = null;
}
<div class="data-group">
<div class="editor-label">
@Html.LabelFor(model => model.Height)
</div>
<div class="option1">
@Html.TextBoxFor(model => model.Height)
</div>
<div class="units">
@Html.DropDownListFor(
model => model.HeightUnit,
Units.Distance,
new { @class = "unit" }
)
</div>
<div class="validation">
<div>
@Html.ValidationMessageFor(model => model.Height)
</div>
</div>
</div>
@使用MyProject.Models;
@模型MyViewModel
@{
布局=空;
}
@LabelFor(model=>model.Height)
@Html.TextBoxFor(model=>model.Height)
@Html.DropDownListFor(
model=>model.HeightUnit,
单位,距离,
新的{@class=“unit”}
)
@Html.ValidationMessageFor(model=>model.Height)
我确实想到了这一点,但这里的问题是,单位变量的名称不同,单位类型也不同。我计划在我的整个站点中使用此模板,但不能仅使用一个或两个值。@JasCav,如果要将dropdownlist绑定到不同的变量,则需要重新调整POST action接收到的视图模型,以便正确绑定所选值。看看HTML源代码中生成的name属性,您应该在POST操作中使用相同的名称来获取值。好的,这是有道理的。我确实查看了这个名称,并看到在本例中所有的名称都命名为“[modelName].unitType”-所以Height.unitType。我不确定我是否理解如何在控制器中绑定它。如果你能提供一个例子,我将不胜感激+1同时,@JasCav,有不同的可能性。第一种方法是设计一个视图模型,其中包含名为UnitType
的字符串属性,然后设计另一个类,该类具有名为Height
的属性,属于第一种模型类型。然后,模型绑定器将自动进行绑定。另一种可能是从请求中获取它:var unitType=request[“Height.unitType”]代码>。谢谢您的回答。不幸的是,我遇到了另一个问题,即即使我设法将正确的结果返回到数据库,DropDownList也无法填充。(大概是因为一开始它没有正确连接。)我可能不得不重新考虑我的解决方案(或者只是手工编写所有东西——不过模板会很好)。