In my web application, there is a couple of partial views
From the Combo box selects the value, related partial views shown and other partial views stays as hidden.
I want to add client-side validation for each partial view. I tried adding validation to each partial view. But it won't work. So need help to solve this. Do I need to add validation on the main view for each partial view or am I doing something wrong in the partial view validations?
This is I did validation for one partial view.
<li style="padding-bottom:15px">
@using (Html.BeginCollectionItem("GeneralItmsList"))
{
@Html.HiddenFor(model => model.TempID)
<div class="form-horizontal" id="quickForm" onsubmit="return validateForm()" name="maind">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-md-5 col-sm-6">
<div class="form-group">
Select Item Description
<div class="col-md-10">
@Html.EditorFor(model => model.Attachment_Description, new { htmlAttributes = new { @class = "form-control", @name = "description" } })
@Html.ValidationMessageFor(model => model.Attachment_Description, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="form-group">
Attachment Amount
<div class="col-md-10">
<div class="input-group-prepend">
<span class="input-group-text">Rs.</span>
@Html.EditorFor(model => model.Attachment_Amount, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Attachment_Amount, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="form-group">
Attachment
<div class="col-md-10">
<input type="file" name="ImageData@(Model.TempID.ToString())" id="ImageData@(Model.TempID.ToString())" multiple="multiple" data-id="Img@(Model.TempID.ToString())" onchange="checkImage(this)" />
@Html.ValidationMessageFor(model => model.Attachment, "", new { @class = "text-danger" })
</div>
@if (Model.Attachment != null)
{
<img src="data:image;base64,@System.Convert.ToBase64String(Model.Attachment)" width="100" height="100" class="ml-1"/>
}
</div>
<img id="Img@(Model.TempID.ToString())" src="" alt="" width="100" height="100" class="ml-1" />
</div>
<button type="button" class="btn btn-danger" onclick="$(this).parent().remove();">Remove</button>
</div>
</div>
}
</li>
<script>
function validateForm() {
let x = document.["maind"]["description"].value;
alert(x);
if (x == "") {
alert("Description must filled");
return false;
}
}
</script>
So I checked this but it won't work.
Here is my main index view. There you can see when user selects a value from the combo box, some partial views visible and others hides. So in this kind of situation where I put validations? I also think we cannot validate every partial view from the index view. But when I try to put validation run on the partial view. It also not work. Can any one help me on this. Thank you.
This is the main view
<div class="col-md-6 col-sm-6">
<div class="form-group row">
@Html.LabelFor(model => model.ReqType, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-sm-8">
@Html.DropDownListFor(model => model.ReqType, ReqTypes, "Select Request Type", new { @class = "js-dropdown" })
@Html.ValidationMessageFor(model => model.ReqType, "", new { @class = "text-danger" })
</div>
</div>
</div>