Model.get返回Null MVC C#

时间:2017-11-20 22:09:52

标签: c# asp.net-mvc

在我的帖子ActionResult Edit上,我收到一个错误。 System.Web.Mvc.WebViewPage<TModel>.Model.get returned null

我的控制器:

[HttpPost]
    public ActionResult Edit(editRequestViewModel _editRequestViewModel, int id)
    {
        try
        {
            if (ModelState.IsValid)
            {
                using (var db = new HelpDeskContext())
                {

                    db.Entry(_editRequestViewModel.userRequest).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();

                    return RedirectToAction("Digest",new { id = _editRequestViewModel.userRequest.ID });
                }
            }

            else
                return View();
        }
        catch (Exception ex)
        {
            return View("Error", new HandleErrorInfo(ex, "Change", "Browse"));
        }
    }

我的视图包含了要绑定的模型字段的内容:

  @Html.DropDownListFor(model => model.userRequest.forApplication, Model.Applications, "Select Application", new { @class = "form-control" })

我的模型将字段设为可为空int?

public int? forApplication { get; set; }

似乎在POST中使用此字段更新了模型中的其他字段。首次创建请求并将其保存到数据库时,它在null时会在该字段中保存正常。在我看来,在发布时可以将nullable作为值(Edit ActionResult)?

编辑:这是我的GET方法,它填充传递给POST的视图模型。

public ActionResult Edit(int id)
    {
        try
        {
            if (ModelState.IsValid)
            {
                using (var db = new HelpDeskContext())
                {
                    var query = (from m in db.Requests
                                 where m.ID == id
                                 select new editRequestViewModel()
                                 {
                                     Applications = (from r in db.Applications
                                                     select new SelectListItem(){
                                                         Text = r.Name,
                                                         Value = r.ID.ToString()
                                                        }).ToList(),
                                     closeReasons = (from r in db.CloseReasons
                                                     select new SelectListItem()
                                                     {
                                                         Text = r.Name,
                                                         Value = r.ID.ToString()
                                                     }).ToList(),
                                     userRequest = m

                                 }).FirstOrDefault();
                    return View(query);
                }
            }

            else
                return View();
        }
        catch (Exception ex)
        {
            return View("Error", new HandleErrorInfo(ex, "Change", "Browse"));
        }
    }

我的视图有@model HelpDeskSolution.ViewModels.editRequestViewModel

编辑2:ViewModel和模型

  namespace HelpDeskSolution.Models
 {
  public class Request
   {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key()]
    public int ID { get; set; }
    [Required]
    [StringLength(99, MinimumLength = 3)]
    public string Title { get; set; }
    [StringLength(1000, MinimumLength = 1)]
    [Required]
    public string Description { get; set; }
    [Required]          
    [Display(Name = "Submit Date")]
    public DateTime SubmitDate { get; set; }
    public DateTime? CloseDate { get; set; }
    [Required]
    [StringLength(30)]
    public string Author { get; set; }
    [Required]
    [StringLength(30)]
    public string AuthDept { get; set; }
    [StringLength(30)]
    [Display(Prompt = "Text at top of Epicor Screen...(Optional)")]
    public string Module { get; set; }
    public int Urgency { get; set; }
    [StringLength(30)]
    public string Type { get; set; }
    public int Status { get; set; }
    [StringLength(30)]
    [Display(Name = "Request For")]
    public string RequestFor { get; set; }
    [Required]
    public bool Closed { get; set; }
    [StringLength(30)]
    [Display(Name = "Assign To")]
    public string AssignedTo { get; set; }
    [Display(Name = "Application")]
    public int? forApplication { get; set; }
    public int? closeReason { get; set; }
    public string ClosedBy { get; set; }
    [Display(Name = "ID")]
    public int? duplicateOf { get; set; }

    }
    }

型号:

 namespace HelpDeskSolution.ViewModels
    {
public class editRequestViewModel
{
    public Request userRequest { get; set; }
    public List<SelectListItem> Applications { get; set; }
    public List<SelectListItem> closeReasons { get; set; }

 }
 }

1 个答案:

答案 0 :(得分:1)

结束了@StephenMuecke的方向解决这个问题。我得到异常的原因是因为在post动作的else部分返回View()时,它试图返回没有Applications列表的视图,正如Stephen所说。然而,这让我意识到模型状态首先存在问题,因此它首先甚至会转向其他地方。我有另一个字段,当它不是一个可以为空的类型时被传递null。

我只是将类型更改为int?并添加了迁移,操作结果为A&#39;现在好了。