验证后,模型中需要参数Null

时间:2017-06-03 12:58:33

标签: c# asp.net asp.net-mvc model-view-controller

我正在使用小表格制作我的mvc应用程序。表单已经过验证,但是在调试时我看到我的字段为空。为什么呢?
我尝试多次调试它,但我仍然无法看到我的错误,无论我做什么,它仍然在验证后为null。

控制器:

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

视图在这里:

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

                            }
                        }

1 个答案:

答案 0 :(得分:1)

您正在使用名为value的视图模型,但之后您尝试从视图模型的不同实例中读取。请尝试阅读“价值”视图模型。