【问题标题】:MVC view wrong validationMVC 视图错误验证
【发布时间】:2016-12-28 02:21:28
【问题描述】:

我正在创建一个 MVC 应用程序。我在这样的控制器中打开一个视图:

return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, id = id });

AddGroupsQty 控制器如下所示:

 public ActionResult AddGroupsQty(int qty, int id)
        {

            var model = new AddGroupsQtyViewModel();
            model.subject_id = id;
            model.qty = qty;
            ClassDeclarationsDBEntities1 entities1=new ClassDeclarationsDBEntities1();
            var subj = entities1.Subjects
                    .Where(b => b.class_id == model.subject_id)
                    .FirstOrDefault();
            model.subject_name = subj.name;
            if (ModelState.IsValid)
            {
                int maxId = 0;
                int total = 0;
                total = entities1.Groups.Count();
                if (total == 0)
                {
                    maxId = 0;
                }
                else
                {
                    maxId = entities1.Groups.Max(u => u.group_id);
                }
                for (int i = 0; i < qty; i++)
                {
                    var teacher = entities1.Users
                    .Where(b => b.email.Replace(" ", String.Empty) == model.teacher_emails[i].Replace(" ", String.Empty))
                    .FirstOrDefault();
                    var group=new Models.Group(id, maxId+1, model.group_names[i], teacher.user_id);
                }
                return RedirectToAction("OperationSuccess", "Account");
            }
            return View(model);
        }

视图模型:

public class AddGroupsQtyViewModel
    {
        public int qty { get; set; }
        public int subject_id { get; set; }
        public string subject_name { get; set; }
        [Required]
        [Display(Name = "Name of group")]
        public List<string> group_names { get; set; }
        [Required]
        [Display(Name = "Email of teacher")]
        [RegularExpression(@"^[a-zA-Z0-9._%+-]+(@student.mini.pw.edu.pl|@mini.pw.edu.pl)$", ErrorMessage = "Registration limited to university domain email.")]
        public List<string> teacher_emails { get; set; }
    }

而且问题是,每当调用视图时,窗口中表单的验证总是有效的。视图如下所示:

@using System.IdentityModel.Configuration
@using System.Web.UI.WebControls
@model ClassDeclarationsThsesis.Models.AddGroupsQtyViewModel

@{
    ViewBag.Title = "AddGroupsQty";
}

<h2>Add Groups to @Model.subject_name</h2>
@if (Model != null)
{
    using (Html.BeginForm("AddGroupsQty", "Account", new { qty = Model.qty, Model.subject_id }, FormMethod.Post, new { @class = "form-horizontal", role = "form", }))
    {
            @Html.AntiForgeryToken()
            <h4>Insert data</h4>
            <hr />
            <table>
                <tr>
                    <th>
                        @Html.ValidationSummary("", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(m => m.group_names, new { @class = "col-md-2 control-label" })
                        </div>
                    </th>
                    <th>
                        @Html.ValidationSummary("", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(m => m.teacher_emails, new { @class = "col-md-2 control-label" })
                        </div>
                    </th>
                </tr>

                @for (int i = 0; i < Model.qty; i++)
                {
                    <tr>
                        <th>
                            <div class="form-group">
                                <div class="col-md-10">

                                    @Html.TextBoxFor(m => m.group_names[i], new { @class = "form-control" })
                                </div>
                            </div>
                           </th>
                        <th>
                            <div class="form-group">
                                <div class="col-md-10">

                                    @Html.TextBoxFor(m => m.teacher_emails[i], new { @class = "form-control" })
                                </div>
                            </div>
                        </th>
                    </tr>
                }
            </table>
            <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,但它应该先验证表单。

【问题讨论】:

  • Pubic ActionResult AddGroupsQty(AddGroupsQtyViewModel value) 试试这样
  • 好的,成功了,谢谢!但是,现在,在填写表格并提交后,我的列表 teacher_emails 和 group_names 都是空的。为什么? @BalajiM

标签: c# .net asp.net-mvc asp.net-mvc-4


【解决方案1】:

验证是基于模型绑定器执行的。所以在控制器动作中,应该是视图模型作为参数。

Public ActionResult AddGroupsQty(AddGroupsQtyViewModel value)

【讨论】:

  • 当您发布表单并想要进行模型验证时,您必须将适当的视图模型作为@Balaji 指定的操作方法参数传递。尝试使用这种方法并在您的代码中进行相关更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多