jjj21
jjj21

Reputation: 37

Null required parameters in model after validation

I am making my mvc app with a small form to fill. The form is validated, but then when debugging I see that my field is null. Why?
I tried to debug it for many times, but I still cannot see my mistake, whatever I do, it still after validation is null.

Controller:

 public ActionResult ViewHomework(ViewHomeworkViewModel value)
        {
            var model = new ViewHomeworkViewModel();
            model.subject_id = value.subject_id;
            ClassDeclarationsDBEntities1 entities = new ClassDeclarationsDBEntities1();
            var subj = entities.Subjects
                    .Where(b => b.class_id == model.subject_id)
                    .FirstOrDefault();
            model.hw2 = entities.Homeworks2.ToList();
            model.users = entities.Users.ToList();
            model.hw_sets = new List<Homeworks2>();
            List<int?> ids = new List<int?>();
            foreach (var hw in model.hw2)
            {
                ids.Add(hw.homework_id);
            }
            ids = ids.Distinct().ToList();

            foreach (var id in ids)
            {
                foreach (var h in model.hw2)
                {
                    if (h.homework_id == id)
                    {
                        model.hw_sets.Add(h);
                        break;
                    }
                }
            }
                if (ModelState.IsValid)
            {
                var deadl = entities.Homeworks2
                   .Where(b => b.deadline == model.deadline)
                   .FirstOrDefault();
                int? id = deadl.homework_id;
                return RedirectToAction("ViewHomeworkView", "Account", new { hw_id = id });
            }
            return View(model);
        }

And the view is here:

@using Microsoft.Owin.Security
@model ClassDeclarationsThsesis.Models.ViewHomeworkViewModel
@{
    ViewBag.Title = "Pick the homework";
}

<h2>@ViewBag.Title.</h2>
<h3>Items marked with * are obligatory.</h3>

@foreach (var user in Model.users)
{
    if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
    {
        if (user.user_type.Replace(" ", String.Empty) == 3.ToString() || user.user_type.Replace(" ", String.Empty) == 2.ToString())

        { using (Html.BeginForm("ViewHomework", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()

                <hr />
                @Html.ValidationSummary("", new { @class = "text-danger" })
                <div class="form-group">
                    @{
                        List<SelectListItem> listItems1 = new List<SelectListItem>();
                    }


                    @foreach (var h in Model.hw_sets)
                    {
                        listItems1.Add(new SelectListItem
                        {
                            Text = h.deadline,
                            Value = h.deadline,
                            Selected = true
                        });
                    }

                    @Html.LabelFor(m => m.deadline, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.deadline, listItems1, new { @class = "form-control" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" class="btn btn-default" value="Submit" />
                    </div>
                </div>

                            }
                        }
                        if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
                        {
                            <p>This window is only accessible to teachers.</p>
                                }

                            }
                        }

Upvotes: 0

Views: 114

Answers (1)

Maciej Miśkiewicz
Maciej Miśkiewicz

Reputation: 412

You are using view model called value, but then you try to read from a different instance of the view model. Please try reading from 'value' view model.

Upvotes: 1

Related Questions