nayan chowdhury nayan chowdhury - 1 year ago 35
ASP.NET (C#) Question

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

I am completely new In .NET.and i am just trying to achieve data annotation validation on client side.For that i search on net and found some solution.
about client side validation.

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

i just follow those steps.my code =>(include js files in bundle(WEB.OPTIMIZATION)) code below---

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


and my view --

@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>


problem is when i just wants to add
@Html.EnableClientValidation()
it showing me some kind of error like below...

Problem
how can i solve my problem.also the steps i am following to achieve data annotation validation on client side i it right/or i need to something else to achive this ---like
@Html.EnableUnobtrusiveJavaScript()

is it necessary to acieve this...
.net MVC expert please help me......

Answer Source

You don't need to set @Html.EnableUnobtrusiveJavaScript() and @Html.EnableClientValidation() in every view, just put this in your web.config:

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

Hope you have set annotation attributes on you model properties. Also, run below script after your form loads:

<script>
$(document).ready(function(){
var form = $('form')//Give Id prop to your from
form.removeData("validator")
form.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
});
<script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download