0
votes

Upon my post ActionResult Edit, I am receiving an error. System.Web.Mvc.WebViewPage<TModel>.Model.get returned null

My controller:

[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"));
        }
    }

My View includes this for the models field to bind:

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

My Model has the field as nullable int?:

public int? forApplication { get; set; }

It seems to update the other fields in the model with this field just fine on POST. When the request is first created and saved to the DB, it saves fine in that field when its null. It seems to me that nullable should be OK as a value when its posting (Edit ActionResult)?

EDIT: This is my GET Method that populates the View Model which is passed in to the 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"));
        }
    }

And my View has @model HelpDeskSolution.ViewModels.editRequestViewModel

EDIT 2: ViewModel and Model

  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; }

    }
    }

Model:

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

 }
 }
1
Model.Applications will be null because you have not populated that property before you return the view - user3559349
@StephenMuecke I am populating that list on the GET actionresult of Edit. It is then passed to the view via a ViewModel in which the drop down is populated and working. - Night Channels
What about userRequest property ? Are you populating that ? - Shyju
Yes, but your not repopulating it in the POST method in the case where you return the view (refer this Q/A). You have not shown your model so its not clear what you issue is, but the fact your using db.Entry(_editRequestViewModel.userRequest) means your view model contains a data model - view models should not contain data models - user3559349

1 Answers

1
votes

Ended up solving this with the direction of @StephenMuecke. The reason I was getting the exception is because upon the return View() in the else portion of the post action, it was attempting to return the view without Applications list, as Stephen said. However that led me to realize that there was first a problem with the Model State, hence why it was even going to the else in the first place. I had another field that was being passed null when it wasnt a nullable type.

I simply changed the type to int? and added a migration and the Action result is A'Okay now.