在MVC中单击“提交”按钮后,为什么数据没有更改?

时间:2018-11-04 19:24:38

标签: c# asp.net-mvc

当我单击“编辑”按钮提交数据时,它只会刷新页面。我已经花了几个小时来解决问题。尽管它不会显示任何错误,并且不会更改数据库。我无法解决为什么会发生的问题。 学生模型班

public class Student
{
    [Key]
    public int StudentId { get; set; }

    [Required]
    [Display(Name = "Student Name")]
    public string StudentName { get; set; }

    [Required(ErrorMessage = "please enter Email")]
    [Remote("IsEmailUnique","Student",ErrorMessage = "This Email is 
                  already Exists")]
    public string Email { get; set; }

    [Required(ErrorMessage = "please fill up Address")]
    [DataType(DataType.MultilineText)]
    public string Address { get; set; }

    [Required]
    public int DepartmentId { get; set; }

    public virtual Department Department { get; set; }

    [Display(Name = "Date of Birth")]
    [Required]
    [DataType(dataType:DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", 
           ApplyFormatInEditMode = true)]
    public  DateTime DoB { get; set; }

    [Required]
    [Remote("IsPhoneUnique", "Student", ErrorMessage = "This Phone is 
             already Exists")]
    public string Phone { get; set; }

    public string Gender { get; set; }

    [Required]
    [Display(Name = "Reg NO")]
    [Remote("IsRegNoUnique", "Student", ErrorMessage = "This RegNo is 
     already Exists")]
    public string RegNo { get; set; }  
}

编辑控制器

[HttpGet]
public ActionResult Edit(int? id)
{
    var student = db.Students.Find(id);

    ViewBag.id = new SelectList(db.Departments, "DepartmentId", " 
              "DepartmentName");

    return View(student);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Student student)
{
    if (ModelState.IsValid)
    {
        db.Entry(student).State = System.Data.Entity.EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    ViewBag.id = new SelectList(db.Departments, "DepartmentId", "DepartmentName", student.DepartmentId);
    return View(student);
}

用于编辑的视图

@model CampusManagementApp.Models.Student
@{
    ViewBag.Title = "Make A Booking";
    HtmlHelper.ClientValidationEnabled = false;
}

<h2>Edit</h2>


@using (Html.BeginForm("Edit", "Student"))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.StudentId)

        <div class="form-group">
            @Html.LabelFor(model => model.StudentName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StudentName)
                @Html.ValidationMessageFor(model => model.StudentName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address)
                @Html.ValidationMessageFor(model => model.Address)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DepartmentId, "DepartmentId", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("DepartmentId",
                (IEnumerable<SelectListItem>)ViewBag.id,
                "Select department")
                @Html.ValidationMessageFor(model => model.DepartmentId)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DoB, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DoB)
                @Html.ValidationMessageFor(model => model.DoB)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Phone, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Phone)
                @Html.ValidationMessageFor(model => model.Phone)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Gender, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <b>Male</b>@Html.RadioButton("Gender", "Male")
                <b>Female</b>
                @Html.RadioButton("Gender", "Female")
            </div>
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

如何解决该问题?

1 个答案:

答案 0 :(得分:2)

您至少有一个验证错误,因为RegNo是必需的,甚至不是视图的一部分。

如评论中所建议,最好是在控制器的这一行上放置一个断点:

if (ModelState.IsValid)

然后检查是否还有其他验证错误...