Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I reuse a remote validation action in MVC3
    text
    copied!<p>I am using a Remote validation attribute on my view model to validate a Bank Account that is specified for my Company:</p> <p>ViewModel:</p> <pre><code>[Remote("CheckDefaultBank", "Company")] public string DefaultBank { </code></pre> <p>This in the controller I have:</p> <pre><code> [HttpGet] public JsonResult CheckDefaultBank(string defaultBank) { bool result = BankExists(defaultBank); return Json(result, JsonRequestBehavior.AllowGet); } </code></pre> <p>That all works well. But, I have two other banks related to my company as well. However, when the remote validation js calls the action it uses a parameter mactching the field name of "DefaultBank"... so I use that as a parameter in my action.</p> <p>Is there some attribute I can add in the view so that it will use a parameter of say "bankId" on the ajax get so I don't need an action for each field which are basically exactly the same?</p> <p>The goal here is to eliminate now having to have this in my controller:</p> <pre><code>[HttpGet] public JsonResult CheckRefundBank(string refundBank) { bool result = BankExists(defaultBank); return Json(result, JsonRequestBehavior.AllowGet); } [HttpGet] public JsonResult CheckPayrollBank(string payrollBank) { bool result = BankExists(defaultBank); return Json(result, JsonRequestBehavior.AllowGet); } </code></pre> <p>I was hoping I could do something like this in the view:</p> <pre><code>@Html.EditorFor(model =&gt; model.DefaultBank, new { data-validate-parameter: bankId }) </code></pre> <p>This way I could just use the same action for all of the Bank entries like:</p> <pre><code>[HttpGet] public JsonResult CheckValidBank(string bankId) { bool result = BankExists(bankId); return Json(result, JsonRequestBehavior.AllowGet); } </code></pre> <p>Possible?</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