Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复杂模型在运行中返回null?_C#_.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 复杂模型在运行中返回null?

C# 复杂模型在运行中返回null?,c#,.net,asp.net-mvc,asp.net-mvc-4,C#,.net,Asp.net Mvc,Asp.net Mvc 4,强类型视图有一个,在提交时,我希望它应该在模型中返回更新的值,但事实并非如此。我不知道发生了什么事 查看模式: public class HomeLoanViewModel { public Lead_Options leadOption { get; set; } public Lead HomeLoanLead {get;set;} public HomeLoanViewModel() { this.leadOption= ne

强类型视图有一个
,在提交
时,我希望它应该在模型中返回更新的值,但事实并非如此。我不知道发生了什么事

查看模式:

public class HomeLoanViewModel
{
    public Lead_Options leadOption { get; set; }       
    public Lead HomeLoanLead {get;set;}

    public HomeLoanViewModel()
    {
        this.leadOption= new Lead_Options();
        this.HomeLoanLead= new Lead();
    }
}
ViewModel中的Lead类如下所示:

public partial class Lead
{
    public string Lead_id { get; set; }      
    public string Income { get; set; }

    [NotMapped]
    public List<SelectListItem> Income_Binder
    {
        get
        {
            return new List<SelectListItem>()
            {
                new SelectListItem(){ Value="", Text="Please select...", Selected=true },
                new SelectListItem(){ Value="$0 - $30,000", Text="$0 - $30,000" },
                new SelectListItem(){ Value="$30,001 - $40,000", Text="$30,001 - $40,000" },
                new SelectListItem(){ Value="$40,001 - $50,000", Text="$40,001 - $50,000"},
                new SelectListItem(){ Value="$50,001 - $60,000", Text="$50,001 - $60,000"},
                new SelectListItem(){ Value="$60,001 - $70,000", Text="$60,001 - $70,000"},
                new SelectListItem(){ Value="$70,001 - $80,000", Text="$70,001 - $80,000"},
                new SelectListItem(){ Value="$80,001 - $90,000", Text="$80,001 - $90,000"},
                new SelectListItem(){ Value="$90,001 - $100,000", Text="$90,001 - $100,000"},
                new SelectListItem(){ Value="$100,001 - $150,000", Text="$100,001 - $150,000"},
                new SelectListItem(){ Value="$150,000+", Text="$150,000+"}
            };
        }
    }
}
视图:

@使用HMC.Common.Utilities;
@模型HMC.Common.ViewModel.HomeLoanViewModel
@{
ViewBag.Title=“LeadContact”;
Layout=“~/Views/_Base.cshtml”;
}
年总收入
@Html.DropDownListFor(m=>m.HomeLoanLead.Income,Model.HomeLoanLead.Income\u Binder,新的{required=”“,aria\u required=“true”,name=“interestateType”})
@HiddenFor(m=>m.HomeLoanLead.Income)
显示顶级住房贷款
编辑:
从上一页/视图请求来提交控制器的操作,然后重定向到LeadContact。这是我传递模型和回发希望接收更新值的视图。

在模型中使用ViewModels或强类型视图时,这种问题很常见。这就是为什么。。。 当回发发生时,模型绑定器最终在控制器被调用回发之前被调用。如果控制器接受类(model或viewmodel)作为输入参数,那么MVC将首先使用null构造函数创建该类的实例。然后,模型绑定器将获取HTML查询字符串(名称-值对),并尝试基于名称-值对填充模型/视图模型属性。如果活页夹找不到与之匹配的正确名称,它就不会做任何事情(这是正确的行为),因此,其症状是“嘿,我缺少我从视图中发布的数据”

调试此操作的最佳方法是1)在控制器的操作方法上设置断点。2) 转到客户端并按F12查看网络日志。3) 启动回发并注意从浏览器发送的名称-值对。在控制器条目处中断后,执行Debug/Windows/Locals并查看控制器参数的模型部分。你会在网络端看到同样的东西(证明他们都回来了)。现在看看类型化的值,如果有什么不应该存在的,那只是因为回发和模型(名称值)不匹配

90%的情况下,根本原因是DisplayFor、HiddenFor或其他“For”元素的第一个参数没有指向要返回的正确内容。这就是我喜欢使用ViewModels的原因

鉴于:

public class HomeLoanViewModel
{
  public Lead_Options leadOption { get; set; }       
  public Lead HomeLoanLead {get;set;}
  public String SelectedValue {get;set;}

  public HomeLoanViewModel()
  {
    this.leadOption= new Lead_Options();
    this.HomeLoanLead= new Lead();
  }
  public void Post(){
     //do all your post logic here
     if(SelectedValue > XYZ) //do this
  }
}
我会像这样创建控制器签名:

 [HttpPost]
public ActionResult Submit(HomeLoanViewModel vm)
{
     if (ModelState.IsValid){
               vm.Post();
               return View(vm);
     }
  return View(vm);
}
@using HMC.Common.Utilities;
@model HMC.Common.ViewModel.HomeLoanViewModel
@{
  ViewBag.Title = "LeadContact";
  Layout = "~/Views/_Base.cshtml";
}
@Using Html.BeginForm(){

    <label>Total Annual Income</label>
     @Html.DropDownListFor(m => m.HomeLoanLead.SelectedValue, Model.HomeLoanLead.Income_Binder, new { required = "", aria_required = "true"})

     <input type="submit/>

}

<div class="formnav row">
 <button class="btn btn-large">Show Top Home Loans <i class="fa fa-chevron-right"></i></button>
</div>
我将创建如下视图:

 [HttpPost]
public ActionResult Submit(HomeLoanViewModel vm)
{
     if (ModelState.IsValid){
               vm.Post();
               return View(vm);
     }
  return View(vm);
}
@using HMC.Common.Utilities;
@model HMC.Common.ViewModel.HomeLoanViewModel
@{
  ViewBag.Title = "LeadContact";
  Layout = "~/Views/_Base.cshtml";
}
@Using Html.BeginForm(){

    <label>Total Annual Income</label>
     @Html.DropDownListFor(m => m.HomeLoanLead.SelectedValue, Model.HomeLoanLead.Income_Binder, new { required = "", aria_required = "true"})

     <input type="submit/>

}

<div class="formnav row">
 <button class="btn btn-large">Show Top Home Loans <i class="fa fa-chevron-right"></i></button>
</div>
@使用HMC.Common.Utilities;
@模型HMC.Common.ViewModel.HomeLoanViewModel
@{
ViewBag.Title=“LeadContact”;
Layout=“~/Views/_Base.cshtml”;
}
@使用Html.BeginForm(){
年总收入
@Html.DropDownListFor(m=>m.HomeLoanLead.SelectedValue,Model.HomeLoanLead.Income\u Binder,新的{required=”“,aria\u required=“true”})

发回时,您的模型没有绑定到您的模型。它需要是
public ActionResult Submit(HomeLoanViewModel model)
,并且在任何情况下,您似乎没有对模型做任何操作。您想做什么?并使用
@使用(Html.BeginForm()){..
在您的视图中,因此
操作
属性correct@StephenMuecke幸运的是,这是您在那里回复的参考。1.使用model I m binding下拉列表,这部分内容很好。2.提交时,我想获取下拉列表的选定值并将其保存在数据库中,但不获取实际的选定值。@StephenMuecke敬请参阅编辑我问题的一部分。您没有传递您提交的模型。您使用
返回视图(“LeadContact”,new HomeLoanViewModel());
正在传递一个新的
HomeLoanViewModel
实例,但这无论如何都不起作用,因为
HomeLoanViewModel
包含复杂对象的属性。您是否希望回发一个模型,保存它,然后重定向到
LeadContact
,以查看其详细信息?