Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Found a way to achieve the functionality by using [Remote] attribute. this is how I have done it.</p> <p>The model CUSTS.cs</p> <pre><code>namespace MvcTest.Models </code></pre> <p>{</p> <pre><code>public partial class CUSTS { public Nullable&lt;double&gt; Field1{ get; set; } [Remote("ValidateAmount", "Validation", AdditionalFields = "Field1, Field2")] [Display(Name = "BALANCE AMT")] public Nullable&lt;double&gt; Field2{ get; set; } } </code></pre> <p>} </p> <p>The controller ValidationController.cs </p> <pre><code>namespace MvcTest.Controllers </code></pre> <p>{ public class ValidationController : Controller { Entities1 db = new Entities1(); public static NameValueCollection messagesCol; public String errorField; public String errorMessage;</p> <pre><code> public JsonResult ValidateAmount(CUSTS custs) { CUSTS cus = new CUSTS(); if (custs.Field2&lt; custs.Field1) { loadMessages(); String[] errMsg = new String[1]; errMsg = messagesCol.GetValues("OES0373"); string st=errMsg[0] + ". \r\n The Balance amount is:" + custs.XWIDV0 + " &amp; the Credit Limit is:" + custs.XWGIVA; return Json(st,JsonRequestBehavior.AllowGet); //return new ActionResult(errMsg[0] + ". \r\n The Balance amount is:" + custs.XWIDV0 + " &amp; the Credit Limit is:" + custs.XWGIVA); } return Json(true, JsonRequestBehavior.AllowGet); //return ActionResult.Success; } public static void loadMessages() { StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~\\Models\\messages_en.properties")); String fileContents = sr.ReadToEnd(); String[] sepr = new string[] { "\r\n" }; String[] lines = fileContents.Split(sepr, StringSplitOptions.RemoveEmptyEntries); messagesCol = new NameValueCollection(); int numMessages = lines.Length; foreach (string line in lines) { int indx = line.IndexOf('='); if (indx != -1) { messagesCol.Add(line.Substring(0, indx), line.Substring(indx + 1)); } } sr.Close(); } } </code></pre> <p>}</p> <p>Now I want to ask two things 1. Is this way of achieving the required functionality is correct. What else could be done. 2. Another Problem that I have just diagnosed is that post method in the form in the view is executing irrespective of the validation whether it is valid or not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload