Reputation: 183
Creating Form in MVC. trying to add validation summary in form.
but it shows validation for all fields.
How to show specific validation summary with asterisk sign?
View
<div class="col-md-12 table-bordered" style="padding: 20px; margin:10px;">
<div class="col-md-12">@Html.ValidationSummary(false, "", new { @class = "text-danger" })</div>
<div class="col-md-12" style="padding:10px;">
<div class="col-md-6">
<div style="width:95%;">
@Html.LabelFor(Model => Model.ItemName, new { style = "font-weight:bold;", @maxlength = "100" })
@Html.TextBoxFor(Model => Model.ItemName)
<span class="text-danger"> @Html.ValidationMessageFor(Model => Model.ItemName) </span>
</div>
</div>
<div class="col-md-6">
<div style="width:75%;">
@Html.LabelFor(Model => Model.ActiveProductGroup, new { style = "font-weight:bold;" })
@Html.DropDownListFor(Model => Model.ProductGroupID, new SelectList(ViewBag.ActiveProductGroup, "Value", "Text"), "--Select Value--", new { style = "Width:95%" })
<span class="text-danger"> @Html.ValidationMessageFor(Model => Model.ActiveProductGroup) </span>
</div>
</div>
</div>
<div class="col-md-12" style="padding:10px;">
<div class="col-md-3">
<div style="width:70%;">
@Html.LabelFor(Model => Model.SequenceNumber, new { style = "font-weight:bold;" })
@Html.TextBoxFor(Model => Model.SequenceNumber, new { style = "text-align:right", Value = "", @maxlength = "3" })
<span class="text-danger"> @Html.ValidationMessageFor(Model => Model.SequenceNumber) </span>
</div>
</div>
<div class="col-md-3">
<div style="width:50%;">
@Html.LabelFor(Model => Model.UnitPrice, new { style = "font-weight:bold;" })
@Html.TextBoxFor(Model => Model.UnitPrice, new { style = "text-align:right", Value = "", @maxlength = "5" })
<span class="text-danger"> @Html.ValidationMessageFor(Model => Model.UnitPrice) </span>
</div>
</div>
<div class="col-md-3">
<div style="width:50%;">
@Html.LabelFor(Model => Model.Quantity, new { style = "font-weight:bold;" })
@Html.TextBoxFor(Model => Model.Quantity, new { style = "text-align:right", Value = "", @maxlength = "5" })
<span class="text-danger"> @Html.ValidationMessageFor(Model => Model.Quantity) </span>
</div>
</div>
<div class="col-md-3">
<div style="width:80%;">
@Html.LabelFor(Model => Model.EstimatedDeliveryDays, new { style = "font-weight:bold;" })
@Html.TextBoxFor(Model => Model.EstimatedDeliveryDays, new { style = "text-align:right", Value = "", @maxlength = "2" })
<span class="text-danger"> @Html.ValidationMessageFor(Model => Model.EstimatedDeliveryDays) </span>
</div>
</div>
</div>
</div>
Screenshot
Want validation summary only for Item Name, Quantity, unit Price and product Group dropdown
Is it possible to get validation summary of specific models?
Upvotes: 2
Views: 506
Reputation: 1222
As per you view your viewmodel should only contain properties that are present in view. ex.ItemName,Product group image, sequence no. etc.
Create another viewmodel that contain only these properties and use it in your view.
However some of the properties that you've used like int32 etc. are always going to be validated by default. if you want to change that behavior make nullable properties for them.
Upvotes: 2