小编ano*_*uar的帖子

View Model在ASP MVC中传递NULL

我正在使用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>" %> …
Run Code Online (Sandbox Code Playgroud)

c# sql-server visual-studio-2010 asp.net-mvc-3

1
推荐指数
1
解决办法
795
查看次数