C# と SQL Server 2005 を使用して ASP .Net MVC 3 アプリケーションを開発しています。
Entity Framework と Code First Method も使用しています。
PartialView を含むビューがあります。つまり、ビューのボタンをクリックすると、部分ビューが表示されます。
ビュー (Index.aspx) とパーシャルビュー (Gestion.ascx) には、私のベースのテーブル 'Gamme' に保存される値を入力するための DropDownList と TextBox が含まれています。
これはビュー 'Index.aspx' です。
<% using (Html.BeginForm("Create", "Anouar"))
{ %>
<div><%:Html.Label("Gamme :")%><%: Html.DropDownList("SelectedProfile_Ga", new SelectList(Model.Profile_GaItems, "ID_Gamme", "ID_Gamme"))%> <input type="button" value="Configurer" id="btnShowGestion" /></div>
<div id="divGestion"><%: Html.Partial("Gestion", Model) %></div>
<% } %>
<script type="text/javascript">
$(document).ready(function () {
// $('#divGestion').load('/Anouar/Gestion');
$('#btnShowGestion').click(function () { $('#divGestion').slideToggle("slow") });
});
</script>
</asp:Content>
これは、ビュー 'Index' に入力するコントローラーです。
public class ProfileGaController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /ProfileGa/
[HttpGet]
public ActionResult Index(Profile_Ga profile_ga, Poste poste)
{
var viewModel = new FlowViewModel();
viewModel.PostesItems = new SelectList(db.Postes.ToList(), "ID_Poste", "ID_Poste");
//viewModel.PostesItems = db.Postes.ToList() ?? new List<Poste>();
viewModel.Profile_GaItems = db.Profil_Gas.ToList();
viewModel.GaItems = db.Gammes.ToList();
return View(viewModel);
}
これは部分ビュー 'Gestion.ascx' です:
<fieldset class="parametrage">
<legend>Gestion de Gamme</legend>
<div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%><input type="checkbox" name="option1" value="Poste Initial" id= "chkMain" onclick="test();"/>Poste Initial<input type="checkbox" name="option2" value="Poste Final" id= "chkFirst" onclick="test2();"/>Poste Final</div>
<div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%></div>
<div><%:Html.Label("Position :")%><%: Html.EditorFor(x=>x.YourGammeModel.Position)%></div>
<div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownList("PostePrecedentSelected", Model.PostesItems)%></div>
<div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownList("PosteSuivantSelected", Model.PostesItems)%></div>
<div><input type="submit" value="Enregistrer" id="btnSave" /></div>
</fieldset>
そして、これは PartialView を生成する Controller です:
public class AnouarController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /Anouar/
public ActionResult Gestion(FlowViewModel model)
{
model.YourGammeModel = new Gamme();
return PartialView(model);
}
[HttpPost]
public ActionResult Create(FlowViewModel model)
{
if (ModelState.IsValid)
{
db.Gammes.Add(model.YourGammeModel);
db.SaveChanges();
return RedirectToAction("Gestion");
}
return View(model.YourGammeModel);
}
}
最後に、これは属性を含む ViewModel です。
public class FlowViewModel
{
[Key]
public string IDv { get; set; }
[NotMapped]
public SelectList PostesItems { get; set; }
public List<Profile_Ga> Profile_GaItems { get; set; }
public List<Gamme> GaItems { get; set; }
public Gamme YourGammeModel { get; set; }
public int SelectedProfile_Ga { get; set; }
public int SelectedGamme{ get; set; }
public int SelectedPoste { get; set; }
public int PostePrecedentSelected { get; set; }
public int PosteSuivantSelected { get; set; }
}
コードを実行すると、常にエラーが表示されます:
ビュー「作成」またはそのマスターが見つからないか、検索された場所をサポートするビュー エンジンがありません。次の場所が検索されました: ~/Views/Anouar/Create.aspx ~/Views/Anouar/Create.ascx ~/Views/Shared/Create.aspx ~/Views/Shared/Create.ascx ~/Views/Anouar/Create. cshtml ~/Views/Anouar/Create.vbhtml ~/Views/Shared/Create.cshtml ~/Views/Shared/Create.vbhtml
説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。
これは、関数 Create にブレークポイントを設定したときのモデルの値に関するスクリーンショットです。