まず、私は MVC と ASP.NET の初心者なので、単純なものが欠けていたら申し訳ありません。
私はコードファーストの MVC 5 アプリケーションに取り組んでいます。簡潔にするために、次のように定義された 2 つのモデルがあるとしましょう。
public class Platform
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "You must select a manufacturer")]
[DisplayName("Manufacturer")]
public int ManufacturerId { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
[Required(ErrorMessage="You must enter a name for the platform")]
[StringLength(50, ErrorMessage="Reduce length to 50 characters or less")]
public string Name { get; set; }
}
と
public class Manufacturer
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="You must enter a name for the manufacturer")]
[StringLength(50, ErrorMessage="Reduce length to 50 characters or less")]
public string Name { get; set; }
public virtual ICollection<Platform> Platforms { get; set; }
}
これらのモデルの「エンティティ フレームワークを使用してビューを含む MVC 5 コントローラー」を作成すると、すべての CRUD アクションが正しく機能します。一方、フォーム要素はフォーマットされておらず、form-control
Bootstrap が必要とするクラスがありません。
EditorTemplate を追加することでタイプごとにこれを回避できますが@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "form-control" })
、Bootstrap MVC 5 プロジェクトの足場はすでに有効なコードを発行しているはずですか? さまざまな MVC 5 チュートリアルを見ると、適切なスタイリングで正しく動作しているように見えるため、何が間違っているのかについて少し混乱しています。
参考までに、各モデルの Create.cshtml ファイルの出力 (簡潔にするために重要なフォーム要素にクリップされています) は次のとおりです。
<div class="form-group">
@Html.LabelFor(model => model.ManufacturerId, "ManufacturerId", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ManufacturerId", String.Empty)
@Html.ValidationMessageFor(model => model.ManufacturerId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
と
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
モデルManufacturerId
に a が設定されている場合、ドロップダウンのラベル テキストとして指定されているのはなぜですか?DisplayName
DropDownList()
強く型付けされた の代わりにドロップダウンが使用されているのはなぜDropDownListFor()
ですか?
前もって感謝します。