如何在Mvc 5中使用远程属性?

时间:2016-04-13 11:57:15

标签: c# asp.net-mvc-5 attributes

我在ASP MVC 5中开发业务应用程序。我试图使用Remote属性,但它不起作用。

元数据类 - 我放置属性

 [MetadataType(typeof(Departmanmetadata))]
    public partial class DepartmanTable
    {

    }
    public class Departmanmetadata
    {
        [Key]
        [DisplayName("Deparman No")]
        public int DepartmanID { get; set; }



        [Required(ErrorMessage = "Bu alan boş bırakılamaz!")]
        [StringLength(50, MinimumLength = 2, ErrorMessage = "Lütfen 2 ile 50 karakter arasında veri giriniz!")]
        [DisplayName("Departman Adı")]
        [Remote("IsNameAvaible", "Departman", ErrorMessage = "Sistemde zaten var!")]
        public string DepartmanName { get; set; }
    }

控制器

 public JsonResult IsNameAvaible(string name)
        {
            return Json(!db.DepartmanTable.Any(a => a.DepartmanName == name), JsonRequestBehavior.AllowGet);
        }

视图

<link href="~/css/eklemeler.css" rel="stylesheet" />

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script src="~/scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/scripts/jquery.validate.min.js"></script>


<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>DEPARTMAN</h2>
        <ol class="breadcrumb">
            <li>
                <a>Home</a>
            </li>
            <li>
                <a>Departman</a>
            </li>
            <li class="active">
                <strong>Departman Add</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">

    </div>
</div>
<br />

    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>Departman Add</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal form-label-left" method="post" action="~/Departman/Create" novalidate>
                         @Html.AntiForgeryToken()
                        <div class="form-horizontal">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div class="item form-group">
                                @Html.LabelFor(model => model.DepartmanName, htmlAttributes: new { @class = "control-label col-md-3 col-sm-3 col-xs-12" })
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    @Html.EditorFor(model => model.DepartmanName, new { htmlAttributes = new { @class = "form-control col-md-7 col-xs-12" } })<span class="fa fa-long-arrow-up form-control-feedback right" aria-hidden="true"></span>
                                    @Html.ValidationMessageFor(model => model.DepartmanName, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <br />
                            <div class="form-group">
                                <div class="col-md-offset-2 col-md-10">
                                    <input type="submit" value="Ekle" class="btn btn-primary" /> |
                                    <i class="fa fa-share fa-2x"></i> @Html.ActionLink("Geri Dön!", "Index")
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

“创建”按钮不起作用。 我认为Scripts就是问题所在。这里是脚本。

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script src="~/scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/scripts/jquery.validate.min.js"></script>

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您应用该属性的属性名为DepartmanName并生成以下html

<input type="text" name="DepartmanName" ..... />

这意味着您的控制器方法必须具有匹配的参数。将其更改为

public JsonResult IsNameAvaible(string departmanName)
{
    ....
}