MVC自动验证

时间:2016-12-29 21:01:44

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

我正在制作我的MVC应用程序。我创建了一个视图,用户从下拉列表中选择数据。视图是这样的:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@using ClassDeclarationsThsesis.Classes
@using Microsoft.Ajax.Utilities
@model ClassDeclarationsThsesis.Models.ClassesViewModel
@{    ViewBag.Title = "Classes";
}


<h2>Classes</h2>

@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("Classes", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Generate summary views</h4>
                <hr />
                @Html.ValidationSummary("", new { @class = "text-danger" })
                <div class="form-group">
                    @{
                        List<SelectListItem> listItems1 = new List<SelectListItem>();
                    }
                    @foreach (var subject in Model.subject)
                    {
                        listItems1.Add(new SelectListItem
                        {
                            Text = subject.name,
                            Value = subject.name,
                        });
                    }
                    @Html.LabelFor(m => m.subject_name, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.subject_name, 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>You do not have enough permissions to enter this page. Contact the administrator.</p>
                                }

                            }
                        }

控制器是:

public ActionResult Classes()
{
    ClassDeclarationsDBEntities1 entities = new ClassDeclarationsDBEntities1();
    var model = new ClassesViewModel();
    model.subject = entities.Subjects.ToList();
    model.users = entities.Users.ToList();
    if (ModelState.IsValid)
    {
        return RedirectToAction("ClassesPickGroup", "Account", new { subject_name=model.subject_name});
    }
    return View(model);
}

模特:

    public class ClassesViewModel
    {
        public List<Subject> subject { set; get; }
        public List<User> users { get; set; }
        [Required]
        [Display(Name = "Subject")]
        public string subject_name { get; set; }

    }

但由于视图只包含一个下拉列表,因此它始终有效并立即重定向到不同的视图。如何让应用程序在下拉列表中等待用户选择,然后提交答案?

1 个答案:

答案 0 :(得分:1)

您的视图中没有问题,但每次用户提交并检查验证时都会创建新对象的问题

<div id="mainDiv">
     <p class ="Call-Customer-Support">Call customer support at 555-555-5555.   </p>
     <div class="Rectangle">
     <img class="call icon-image" src="images/call.png" />
     <a class="Call-Support" href="tel:555-555-5555">Call Support</a>
     </div>
</div>



if (<% Request.QueryString["mode"] %> == "showdiv") {
var item =  getElementById('mainDiv');
item.style.display = 'none';
}else{
item.style.display = 'block';
}