远程注释不工作ASP .NET MVC5

时间:2016-04-22 11:24:10

标签: asp.net-mvc

我遇到远程注释问题。它不起作用。

值得一提的是,我将属性名称从Code更改为DepartmentCode,将Name更改为DepartmentName。从那时起它就没有用了,虽然我做了所有必要的修改。

请帮助我了解原因以及如何解决。

提前谢谢。

以下是模型:

public class Department
{
    [Key]
    [Required(ErrorMessage = "Required")]
    [Remote("IsCodeExists", "Departments", ErrorMessage = "Department Code is in use. Please try new code")]
    [StringLength(7, ErrorMessage = "Must be between {2} and {1} characters long.", MinimumLength = 2)]
    [DisplayName("Department Code")]
    public string DepartmentCode { get; set; }
    [Required(ErrorMessage = "Required")]
    [Remote("IsNameExists", "Departments", ErrorMessage = "Department Name is in use. Please try new name")]
    [DisplayName("Department Name")]
    public string DepartmentName { get; set; }
}

这是控制器:

public class DepartmentsController : Controller
{
    private ApplicationContext db = new ApplicationContext();


    // GET: Departments
    public ActionResult ShowAllDepartments()
    {
        return View(db.Departments.ToList());
    }

    // GET: Departments/Create
    public ActionResult Create()
    {
        return View();
    }


    // POST: Departments/Create
    public JsonResult IsCodeExists(string code)
    {
        return Json(!db.Departments.Any(x => x.DepartmentCode == code), JsonRequestBehavior.AllowGet);
    }
    public JsonResult IsNameExists(string name)
    {
        return Json(!db.Departments.Any(x => x.DepartmentName == name), JsonRequestBehavior.AllowGet);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "DepartmentCode,DepartmentName")] Department department)
    {
        ViewBag.Message = "Department Not saved";
        ViewBag.Status = "Error";

        if (ModelState.IsValid)
        {
            try
            {
                db.Departments.Add(department);
                db.SaveChanges();
                ViewBag.Status = "Success";
                ViewBag.Message = "Department Saved Successfuly";

            }
            catch (Exception)
            {
                ViewBag.Status = "Error";
                ViewBag.Message = "Department Code and Name required";
            }

            ModelState.Clear();
        }


        return View();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

以下是视图:

@model UniversityApplication.Models.Department

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>  


@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()


<div class="form-horizontal">
    <h4>Department</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.DepartmentCode, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DepartmentCode, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DepartmentCode, "", new { @class = "text-danger" })
        </div>
    </div>

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

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

    @*<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @if (ViewBag.Status == "Success")
            {
                <p class="text-success"> @ViewBag.Message</p>
            }
            else if (ViewBag.Status == "Error")
            {
                <p class="text-danger"> @ViewBag.Message</p>
            }
        </div>
    </div>*@

</div>
}

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

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

}

0 个答案:

没有答案