不能隐含地转换类型' void'对象' asp.net.MVC

时间:2017-05-23 06:42:58

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我在.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()时,它会向我显示某种错误,如下所示......

Problem 我怎样才能解决我的问题。另外我要遵循的步骤在客户端实现数据注释验证我是对的/或者我还需要其他东西来实现这一点 - 比如@Html.EnableUnobtrusiveJavaScript() 有必要解决这个问题...... .net MVC专家请帮帮我......

2 个答案:

答案 0 :(得分:3)

您不需要在每个视图中设置@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>

答案 1 :(得分:0)

看看你可以像这样添加这个代码

@(ViewContext.ClientValidationEnabled = true)

或者您可以获得与以下相同的结果:

@{ Html.EnableClientValidation(); }

或者在mvc中你不需要在视图页面上提到这个,你只需将它添加到web配置文件中,如下所示:

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