繁体   English   中英

无法将类型'void'隐式转换为'object'asp.net.MVC

[英]cannot implicitly convert type 'void' to 'object' asp.net.MVC

我是.NET中的全新用户,我只是想在客户端实现数据注释验证。为此,我在网上搜索并找到了解决方案。 关于客户端验证。

https://www.codeproject.com/Articles/556995/ASP-NET-MVC-interview-questions-with-answers

我只是按照这些步骤操作。我的代码=>(将js文件包含在bundle(WEB.OPTIMIZATION)中)-

.Include("~/Content/js/plugins/jquery/jquery-2.2.4.js")
.Include("~/Scripts/jquery.validate.js")
.Include("~/Scripts/jquery.validate.unobtrusive.js")

和我的看法-

        @using something
        @model Vendor
        @{
            ViewBag.Title = "Add Vendor";
            ViewBag.Description = "TSMS-admin";
        }

        @section PageDescription{
            <section class="content-header">
                <h1>
                    @ViewBag.Title
                    <small>@ViewBag.Description</small>
                </h1>
            </section>
        }

        @section Scripts{
            @if (ViewBag.Message != null)
            {
                <script type="text/javascript">
                    $(function () {
                        CustomMessage('Error', '@ViewBag.Message', 'Close');
                    });
                </script>
            }
        }


        <div class="row .col">
            <div style="margin-top:20px" class="mainbox col-md-12 col-md-offset-0 col-sm-8 col-sm-offset-2">
                @using (Html.BeginForm("Add", "Admin", FormMethod.Post, new { @class = "form-horizontal", @id = "AddVendorForm", @enctype = "multipart/form-data" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.EnableClientValidation()
                    @Html.ValidationSummary(true, null, new { @class = "text-danger" })
                    @Html.HiddenFor(model => model.adding_date, new { @Value = System.DateTime.Now })

                    <div class="panel panel-info">
                        <div class="panel-heading">
                            <div class="panel-title">Add A New Vendor</div>
                        </div>
                        <div class="panel-body col-md-12">
                            <div>
                                <img class="center-block" style="border-radius: 3px;height: 200px;width: 200px;" id="venpic" src="~/Content/img/tsms/default/upload.png"><br><br /><br />
                                <label style="border-radius: 2px;" class="col-xs-12 btn btn-primary btn-load btn-lg" data-loading-text="Uploadinging Picture...">
                                    <input autocomplete="off" required name="pic_path" id="ifile_img" accept="image/gif,image/jpeg,image/jpg" onchange="readURL(this,'#venpic')" type="file" style="display: none;">Choose Pic
                                </label>
                                @Html.ValidationMessageFor(model => model.pic_path, null, new { @class = "text-danger" })
                            </div>
                            <br /><br /><br />
                            <div>
                                @Html.TextBoxFor(model => model.heading, new { @style = "border-radius:3px;", @class = "form-control", @id = "VendorName", @placeholder = Html.DisplayNameFor(model => model.heading), @autocomplete = "on", @onkeyup = "uppercase(this)" })
                                @Html.ValidationMessageFor(model => model.heading, null, new { @class = "text-danger" })
                            </div>
                            <br />
                            <div>
                                @Html.TextAreaFor(mode => mode.body, new { @style = "border-radius:3px;", @class = "form-control", @id = "VendorDetails", @placeholder = Html.DisplayNameFor(model => model.body), @autocomplete = "on" })
                                @Html.ValidationMessageFor(model => model.body, null, new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="panel-footer">
                            <div class="panel-title">
                                <div class="form-actions no-color">
                                    <input onclick="d()" type="button" id="AddVendor" value="Add" class="btn btn-success" /> | @Html.ActionLink("Back to List", "Manage")
                                </div>
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>

问题是当我只想添加@Html.EnableClientValidation()它向我显示了某种如下所示的错误...

问题 我该如何解决我的问题。我在客户端上实现数据注释验证所遵循的步骤是正确的/或者我需要其他一些措施来实现这一点-例如@Html.EnableUnobtrusiveJavaScript()是否有必@Html.EnableUnobtrusiveJavaScript() .... net MVC专家请帮助我......

您不需要在每个视图中都设置@Html.EnableUnobtrusiveJavaScript()@Html.EnableClientValidation() ,只需将其放在您的web.config中:

<appSettings>
 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

希望您在模型属性上设置了注释属性。 此外,在加载表单后,在脚本下方运行:

<script>
$(document).ready(function(){
var form = $('form')//Give Id prop to your from
form.removeData("validator")
form.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
});
<script>

看起来您可以像这样添加此代码

@(ViewContext.ClientValidationEnabled = true)

或者您可以达到以下相同的结果:

@{ Html.EnableClientValidation(); }

或者在mvc中,您无需在视图页面上提到此,只需将其添加到Web配置文件中即可,如下所示:

<appSettings>
   <add key="ClientValidationEnabled" value="true" />
   <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM