私がそれを行う方法は、厳密に型付けされた "アドレス"用の表示/エディタテンプレートを作成することです。
その後、あなたはまた、異なる用途のために別のテンプレートを作成することができます@Html.EditorFor(x=>x.BusinessAddress)
または@Html.DisplayFor(x=>x.BusinessAddress)
を呼び出し、その後、必要に応じて編集/ DisplayFor方法でテンプレートの名前を指定します。
ところで、私が言及したテンプレートは〜Views/Shared/EditorTemplatesまたは〜Views/Shared/DisplayTemplatesフォルダにそれぞれ存在する可能性があります。または、EditorTemplatesまたはDisplayTemplatesの下の特定のControllersビューフォルダに常駐することもできます。
編集:ここにこれを示すリンクがあります。 Editor Tempates
編集2:ここでは試してみて、あなたのコメントにつき、さらに説明するための例です。
アドレスモデル
public partial class Address
{
public int AddressId { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
エディタテンプレート:このファイルは、 "Address.cshtml" という名前と私の "〜ビュー/共有/ EditorTemplates" フォルダにあります。
@model Address
@Html.HiddenFor(model => model.AddressId)
<div class="editor-label">
@Html.LabelFor(model => model.Street1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Street1)
@Html.ValidationMessageFor(model => model.Street1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Street2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Street2)
@Html.ValidationMessageFor(model => model.Street2)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
@* Populated from Helper *@
@Html.DropDownListFor(m => m.State, new SelectList(statesDictionary, "Key", "Value"), string.Empty)
@Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Zip)
@Html.ValidationMessageFor(model => model.Zip)
</div>
ビューを作成します。これは私のモデルのエディタテンプレートを実装します私の見解です。私のビューのための私のモデルは、「住所」にリンクされている、何か他のものだった場合今、私は名前については@Html.EditorFor(model => model.BusinessAddress)
を使用しているだろう@Html.EditorForModel();
@model Address
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Address</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
ラインに注意してください、それはあなたのコントロール下で完全です。デフォルトでは、それらはプロパティ名にマッピングされますが、EditorFor
オーバーロード内にhtml属性を含むオブジェクトを指定することで変更できます。
私は答えようとしますが、サンプルコードを見直して編集する必要があると思います。現在のところあなたの物語には従いません。 – trebormf