jquery - how add ValidationMessageFor to view with list of models -
i need add validation mvc5 form. have added required model , working other pages page different. in view used customermodel, may return more 1 value model @ top of page list of customermodel. when wanted have line validation not sure how should write it. working 1 value this:
@html.validationmessagefor(model => model.account_number); but model list , tried gave me error:
@html.validationmessagefor(model => model[1].account_number); this complete view:
enter code here @model list<csaproject.models.customermodel> @{ viewbag.title = "search account number"; } @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <table style="border: none"> <tr> <td colspan="2" style="text-align: center; font-weight: bold; color: red">@*by remoterefnumber*@<br /> </td> </tr> <tr> <td style="text-align: center; padding: 5px;">enter account number: </td> <td style="text-align: center; padding: 5px;"> @html.textbox("account_number") @html.validationmessagefor(model => model[1].account_number); </td> <td colspan="2" style="text-align: center; font-weight: bold; color: red"> <button type="submit" class="btn btn-success">search</button></td> </tr> </table> } @if (model.count > 0) { <table class="table"> <tr> <th>account number </th> <th>first name </th> <th>last name </th> <th>last 4ssn </th> <th>zip </th> <th>is registered </th> <th>online agreement </th> </tr> @foreach (var customer in model) { <tr> <td> @customer.account_number </td> <td> @customer.first_name </td> <td> @customer.last_name </td> <td> @customer.last4ssn </td> <td> @customer.mailing_zip </td> <td> @if (customer.isregistered == true) { <text>yes</text> } else { <text>no</text> } </td> <td> @if (customer.online_agreement == true) { <text>yes</text> } else { <text>no</text> } </td> <td>@* name of button, action name,name of controler,parameter of action,class*@ @html.actionlink("edit", "edit", "customerdetail", new { id = customer.customer_id }, new { @class = "btn btn-success" }) </td> </tr> } </table> } <script src="~/scripts/jquery-1.10.2.min.js"></script> <script src="~/scripts/jquery.validate.min.js"></script> <script src="~/scripts/jquery.validate.unobtrusive.min.js"></script> and model:
public class customermodel { public int customer_id { get; set; } [required] [displayname("account number")] public string account_number { get; set; } [displayname("first name")] public string first_name { get; set; } [displayname("last name")] public string last_name { get; set; } , rest } and controller:
public actionresult searchbyaccountnumber(inputdataaccount model) { customerserviceuri = configurationmanager.appsettings["url"].tostring(); list<customermodel> customermodels = new list<customermodel>(); if (modelstate.isvalid) { if (model.account_number != null) { var uri = string.format("{0}getcustomerbyaccountnumber", customerserviceuri); // create object convert json var data = new { customerdata = new { account_number = model.account_number } }; // convert object byte[] var datatosend = encoding.utf8.getbytes(jsonconvert.serializeobject(data)); // create request web service var req = httpwebrequest.create(uri); //meta data req.contenttype = "application/json"; req.contentlength = datatosend.length; req.method = "post"; // write bytes web service (post data) req.getrequeststream().write(datatosend, 0, datatosend.length); // call web service posted data var response = req.getresponse(); // create string container json response string jsonresponse = string.empty; using (stream responsestream = response.getresponsestream()) { using (streamreader streamreader = new streamreader(responsestream)) { jsonresponse = streamreader.readtoend(); } } // convert json our response our response object "searchbynameresult" var jsonreult = jsonconvert.deserializeobject<searchbyaccountnumberresult>(jsonresponse); // cutomers value of result property var customers = jsonreult.getcustomerbyaccountnumberresult; // create customermodels our results foreach (var customer in customers) { customermodels.add(new customermodel { customer_id = convert.toint32(customer.customer_id), account_number = customer.account_number, first_name = customer.first_name, last_name = customer.last_name, isregistered = customer.isregistered, online_agreement = customer.online_agreement, last4ssn = customer.last4ssn, mailing_zip = customer.mailing_zip }); } } } return view(customermodels); }
Comments
Post a Comment