Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在MVC中单击“提交”按钮后数据不更改?_C#_Asp.net Mvc - Fatal编程技术网

C# 为什么在MVC中单击“提交”按钮后数据不更改?

C# 为什么在MVC中单击“提交”按钮后数据不更改?,c#,asp.net-mvc,C#,Asp.net Mvc,当我点击编辑按钮提交数据时,它只是刷新页面。我花了几个小时来解决这个问题。虽然它没有显示任何错误,也没有更改数据库。我无法解决为什么会发生这种情况的问题。 学生示范班 public class Student { [Key] public int StudentId { get; set; } [Required] [Display(Name = "Student Name")] public string StudentName { get; set;

当我点击编辑按钮提交数据时,它只是刷新页面。我花了几个小时来解决这个问题。虽然它没有显示任何错误,也没有更改数据库。我无法解决为什么会发生这种情况的问题。 学生示范班

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; }  
}
编辑控制器

查看以进行编辑

@model CampusManagementApp.Models.Student
@{
ViewBag.Title=“预订”;
HtmlHelper.ClientValidationEnabled=false;
}
编辑
@使用(Html.BeginForm(“编辑”、“学生”))
{
@Html.AntiForgeryToken()
大学生

@Html.ValidationSummary(true) @Html.HiddenFor(model=>model.StudentId) @LabelFor(model=>model.StudentName,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.StudentName) @Html.ValidationMessageFor(model=>model.StudentName) @Html.LabelFor(model=>model.Email,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Email) @Html.ValidationMessageFor(model=>model.Email) @LabelFor(model=>model.Address,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Address) @Html.ValidationMessageFor(model=>model.Address) @LabelFor(model=>model.DepartmentId,“DepartmentId”,新的{@class=“controllabel col-md-2”}) @Html.DropDownList(“部门ID”, (IEnumerable)ViewBag.id, “选定部门”) @Html.ValidationMessageFor(model=>model.DepartmentId) @LabelFor(model=>model.DoB,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.DoB) @Html.ValidationMessageFor(model=>model.DoB) @LabelFor(model=>model.Phone,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Phone) @Html.ValidationMessageFor(model=>model.Phone) @LabelFor(model=>model.Gender,新的{@class=“controllabel col-md-2”}) Male@Html.RadioButton(“性别”、“男性”) 女的 @单选按钮(“性别”、“女性”) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }

如何解决该问题?

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

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

if (ModelState.IsValid)

并检查是否存在任何其他验证错误…

我猜是
ModelState。IsValid
返回
false
。在您的HttpPost操作方法中放置一个断点并进行验证。无论它们看起来有多像桌面GUI,ASP.Net应用程序仍然是1980 HTML WebFormular。设计师们40年前做出的那些糟糕的决定仍然完全有效。如果您有任何问题,通常最好从了解HTML限制开始。以及您如何处理这些问题。我们需要了解更多代码。具体来说,HTML表单是什么样子,学生模型类是什么样子。否则,很难说问题出在哪里。但很可能ModelState.IsValid会像Shyju所说的那样返回false。请显示您正在发布到服务器的学生模型,并且您的视图(如上所示)不包含学生模型。您的视图也未显示防伪令牌的存在。已添加学生的模型类。在“编辑”视图中添加regNo后,该类仍然有效,谢谢
@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")
}
if (ModelState.IsValid)