ano*_*uar 1 c# sql-server visual-studio-2010 asp.net-mvc-3
我正在使用C#和SQL Server 2005开发ASP .Net MVC 3应用程序.我还使用了Entity Framework和Code First Method.我有一个视图'索引',其中包含指向另一个视图'更新'的链接.在调试中,视图的值为NULL的问题.
该视图由名为ProfileGa的控制器填充.
这是控制器ProfileGa的一部分代码:
public ActionResult Update(string id)
{
FlowViewModel flv = db.FlowViewModels.Find(id);
return View(flv);
}
[HttpPost]
public ActionResult Update(FlowViewModel model)
{
Console.WriteLine("" + model.Nbr_Passage);
Gamme G = new Gamme();
ListG.Remove(G);
db.Gammes.Remove(G);
G.ID_Gamme = model.SelectedProfile_Ga;
G.ID_Poste = model.SelectedPoste;
G.Last_Posts = model.PostePrecedentSelected;
G.Next_Posts = model.PosteSuivantSelected;
G.Nbr_Passage = int.Parse(model.Nbr_Passage);
G.Position = int.Parse(model.Position);
ListG.Add(G);
db.Gammes.Add(G);
db.SaveChanges();
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
当我在线上放置一个断点时:
FlowViewModel flv = db.FlowViewModels.Find(id);
Run Code Online (Sandbox Code Playgroud)
flv的值为NULL.也许在数据库中没有指定id的记录.
我不能深入研究方法Find,因为这个方法的代码是预定义的(我没有写它).它是DbSet的一种方法.
相同的代码正在与其他视图一起使用,但仅限于此.
我认为问题取决于ID(ID_Gamme),它是DropDownList,不像其他人(TextBox).
这是视图索引的一部分代码,其中包含视图更新的链接:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>
<%@ Import Namespace="Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table>
<% foreach (var item in Model.GaItems) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.ID_Gamme) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.ID_Poste) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Nbr_Passage) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Position) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Last_Posts) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Next_Posts) %>
</td>
<td>
<%: Html.ActionLink("Modifier", "Update", new { id=item.ID_Gamme }) %>
</td>
</tr>
<% } %>
</table>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
这是模型FlowViewModel的代码:
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 string SelectedProfile_Ga { get; set; }
public string SelectedPoste { get; set; }
public string PostePrecedentSelected { get; set; }
public string PosteSuivantSelected { get; set; }
public string Position { get; set; }
public string Nbr_Passage { get; set; }
public List<Gamme> ListG = new List<Gamme>();
}
Run Code Online (Sandbox Code Playgroud)
Gestion.ascx的一部分:
<% using (Html.BeginForm("Save", "Anouar")) { %>
<%: Html.ValidationSummary(true) %>
<fieldset class="parametrage">
<legend>Gestion de Gamme</legend>
<div><%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems)%></div>
<div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(model=>model.Nbr_Passage)%></div><div class="editor-field"> <%: Html.ValidationMessageFor(model => model.Nbr_Passage)%></div>
<div><%:Html.Label("Position :")%><%: Html.EditorFor(model=>model.Position)%></div>
<div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownListFor(model => model.PostePrecedentSelected, Model.PostesItems)%></div>
<div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownListFor(model => model.PosteSuivantSelected, Model.PostesItems)%></div>
<div><input type="submit" value="Enregistrer" id="btnSave" /></div>
</fieldset>
...
Run Code Online (Sandbox Code Playgroud)
问题是因为FlowViewModel它不是数据库中的实体,而是ViewModel.因此,当您尝试查询数据库时,FlowViewModel.Find...它没有任何内容可以返回,因为它没有映射到数据库表.
您可能希望从其他表中获取各种信息,例如Gamme(您要写回).您应该FlowViewModel从其他数据库实体填充,然后传递到View.
所以,要填充的Profile_GaItems和Gammes数据库实体做这样的事情:
public ActionResult Update(string id)
{
FlowViewModel flv = new FlowViewModel();
flv.Profile_GaItems = db.Profile_GaItems.ToList();
flv.GaItems = db.Gammes.ToList();
return View(flv);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
795 次 |
| 最近记录: |