简体   繁体   English

使用Web Api控制器验证表单

[英]Validate a form using Web Api controller

I have a web api application in which I have this form : 我有一个具有以下形式的Web API应用程序:

     <form id="editForm">

                    <input id="editid" type="hidden" name="id" />
                    <p><label>Numéro cnss </label><input id="cnss" name="cnss" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Nom </label><input id="lastname" name="lastname" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Prénom </label><input id="firstname" name="firstname" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Adresse   </label> @Html.TextArea("address", new { id = "address" })<label style="color:red" id="labError"> ( * )</label></p>

                    <p><label>Lieu de naissance </label><input id="birth_place" name="birth_place" /> </p>
                    <p><label>Mutuelle </label><input id="mutuelle" name="mutuelle" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Caisse d'assurance maladie </label><input id="caisse_assu_maladie" name="caisse_assu_maladie" /><label style="color:red" id="labError"> ( * )</label></p>

                    <p>
                        <label>Type de client   </label>
                        <select id="client_type" name="client_type">
                            <option value="demande de transport régulière sur une période">demande de transport régulière sur une période</option>
                            <option value="demande de transport régulière et indéfinie">demande de transport régulière et indéfinie</option>
                            <option value="Ponctuel ">Ponctuel </option>
                        </select>
                    </p>

                </form>
 <button id="btnStepTwo" type="submit" class="btn btn-primary" >Commencer la réservation</button>

in the Api controller, I have this action : 在Api控制器中,我有以下操作:

 [HttpPost]
        public bool ValidateClient(ClientModel item)
        {
            return ModelState.IsValid;  
        }

I'd like to add a javascript method for every change in the form, this method calls the ValidateClient service to verify if the model in the form is valid or not : If the model is valid ==> the button will be enabled, otherwise it will be disabled. 我想为表单中的每个更改添加一个javascript方法,此方法调用ValidateClient服务以验证表单中的模型是否有效:如果模型有效==>,将启用该按钮,否则它将被禁用。

  1. How can I accomplish this task? 如何完成这项任务?
  2. What is the best and easiest way? 最好和最简单的方法是什么?

You attach a change() event to the inputs: 您将change()事件附加到输入:

$('input').on('change', function (e) {
    var el = $(e.currentTarget);
    $.post(url, $('form').serialize(), function (data) {
        if (data) {
            // do stuff to "el"
        }
    });
});

From a REST perspective, you may want to respond with a proper HTTP code. 从REST的角度来看,您可能希望使用正确的HTTP代码进行响应。 Not sure a 200 response makes sense if the model is invalid. 如果模型无效,不确定200响应是否有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM