【发布时间】:2013-05-11 17:08:14
【问题描述】:
我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。
我也在使用实体框架和代码优先方法。
我有一个包含 PartialView 的视图。这意味着当我单击视图中的按钮时,会出现局部视图。
视图 (Index.aspx) 和部分视图 (Gestion.ascx) 包含 DropDownList 和 TextBox 以填充将保存在我的基础中的表“Gamme”中的值。
这是视图“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>
这是填充视图“索引”的控制器:
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 的控制器:
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
描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
这是我在函数 Create 处设置断点时关于模型值的屏幕截图:
【问题讨论】:
-
您在 MVC 3 应用程序中使用贬低的 .aspx 和 .ascx 代替 razor 代码是否有原因?
-
是的,因为我使用的模板不仅仅适用于 aspx 或 ascx
-
执行是指提交表单吗?如果是,我猜您会收到该错误,因为
ModelState无效,或者model.YourGammeModel不是您的视图所期望的。 -
是的执行意味着提交表单!我不知道,,,我会尝试一个断点并给你结果
-
@anouar - 不,因为我无法理解你的问题。你的整个问题对我来说毫无意义。
标签: c# asp.net-mvc-3 visual-studio-2010