c# - How to add @Html.ValidationMessageFor for each item in a collection? -
How do you add @ html.ValidationMessageFor ()
for each item archive
? Say,
public class FooVm {// some property public isloping & lt; Barvm & gt; Bar {receive; Set; }} Public Square BarVm {// Some property [range (1, Int.Max, ErrorMessage = "Should be greater than 1") Public float? Fox {Received; Set; }}
Then a view
@model namespace. Here.FooVm & lt; Div class = "container" & gt; & Lt; / Div & gt; & Lt; A href = "#" class = "trigger" & gt; Populate & lt; / A & gt; & Lt; Script & gt; $ (Function () {var i = 0; var populate = function} {var strbuilder = '& lt; input type = "text" name = "bar [i] .fx" />' $ $ (" .contenter ") .append (strbuilder); Return Back;}; $ (" Trigger "). Click (Populate);}); & Lt; / Script & gt;
This is all working but how can I add validation to every textbox
? I am using asp.NET MVC 4
I am also using untrusted verification
for client verification. Any kind of suggestions or suggestions, sample code would be great. Thank you. Basically, using Javascript to populate a scene, MVC should not be used. Instead, you can present all text boxes in such a way:
Code for the first class:
public class FooVm {// Some property public list & Lt; Barvm & gt; Bar {receive; Set; } Public FooVm () {// Make sure the collection exists to prevent NullReferenceException. Bars = new list & lt; BarVm & gt; (); }} Public Square BarVm {// Some property [Category (1, Int32.MaxValue, ErrorMessage = "should be greater than 1")) Public float? Fox {Received; Set; }}
Code for now view:
@ Model Web App 2 Model.fovim & lt; H2 & gt; View sample & lt; / H2 & gt; @Using (HTML.benform ("Your Action", "Your Controller")) {& lt; Div class = "container" & gt; For @ (Int I = 0; I and Lt; Model. Beers.Count; I ++) {@ HTML. Textbox (M = & gt; M.rs [I] .fx) @HTML ValidationMessgefer (M = & gt; M.bers [I] .fox); } & Lt; / Div & gt; }
This will provide the necessary tags - and of course verification messages-bits. However, it is also possible to add all error messages in the same location using
Prefix @ html.ValidationSummary ()
if you actually display stuff After just clicking on a button, consider using partial views and loading that one by using Javascript, it is a better way by using all the necessary tags and attributes for verification.
Regards,
Frank
Comments
Post a Comment