gpt4 book ai didi

c# - 在 ASP MVC 中传递 NULL 的值

转载 作者:行者123 更新时间:2023-12-02 05:04:15 26 4
gpt4 key购买 nike

我正在使用 C# 和 SQL Server 2005 开发 ASP .Net MVC 3 应用程序。

我也在使用 Entity Framework 和 Code First 方法。

我想从 DropDownList 中的表(从我的库)上传数据。

表格是“Poste”,它有一个模型“Poste”。

显示 DropDownList 的 View 是“Gestion”及其代码:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>

<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset class="parametrage">
<legend>Gestion de Gamme</legend>

<div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", new SelectList(Model.PostesItems, "ID_Poste", "ID_Poste"))%></div>


<div><%:Html.Label("Nombre de Passage :")%></div>
<div><%:Html.Label("Position :")%></div>
<div><%:Html.Label("Poste Précédente :")%></div>
<div><%:Html.Label("Poste Suivante :")%></div>

</fieldset>
<% } %>

如您所见,它是从 FlowViewModel 导入的,代码为:

namespace MvcApplication2.Models
{
public class FlowViewModel
{
[Key]
public string IDv { get; set; }
public List<Poste> PostesItems { get; set; }
public List<Profile_Ga> Profile_GaItems { get; set; }
public List<Gamme> GaItems { get; set; }

public int SelectedProfile_Ga { get; set; }

public int SelectedGamme{ get; set; }

public int SelectedPoste { get; set; }
}
}

这是返回 View 的 Controller :

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 = db.Postes.ToList();
viewModel.Profile_GaItems = db.Profil_Gas.ToList();
viewModel.GaItems = db.Gammes.ToList();
return View(viewModel);



}

执行中出现异常的问题,其中提到值正在传递 NULL : error

最佳答案

您可以为 FlowViewModel 创建构造函数并初始化 PostesItems = new List<Poste>() ,因此如果没有任何项目,它将是一个空列表而不是 null。

编辑,如果不清楚,试试改FlowViewModel对此-

    public class FlowViewModel
{
public FlowViewModel()
{
PostesItems = new List<Poste>()
}

[Key]
public string IDv { get; set; }
public List<Poste> PostesItems { get; set; }
public List<Profile_Ga> Profile_GaItems { get; set; }
public List<Gamme> GaItems { get; set; }

public int SelectedProfile_Ga { get; set; }

public int SelectedGamme{ get; set; }

public int SelectedPoste { get; set; }
}

再次编辑:在聊天中解决了这个问题。 Index 操作正在调用一个工作正常的 Index View ,但是 那个 View 试图使用 Javascript 加载一个不同的 View ,而不是传入一个模型,所以它是模型屏幕截图中为空。

关于c# - 在 ASP MVC 中传递 NULL 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com