Saif
Saif

Reputation: 2679

using java script and JQuery to show message instead of throwing exception

How to show message(to inform user if group is added successfully or not) using Javascript and JQuery instead of throwing an erro. Actually this code check if group name already exist in database.

Controller :

[HttpPost]

    public int CreateGroup(UserGroup group)
    { 

        return bc.Create(group, user.id);
    }

User group class:

    UserGroupDalc Dalc = new UserGroupDalc();

    public int Create(UserGroup group, int creatorId)
    {

     if(ByName(group.name) != null) throw new ArgumentException(string.Format("Group name: {0} is already exist.", group.name));
     return Dalc.CreateGroup(group, creatorId);
    }

User group dalc class:

public int CreateGroup(UserGroup group, int creatorId) {

            connection();
             com = new SqlCommand("spp_adm_user_group_ins", conn);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@name", group.name);
            com.Parameters.AddWithValue("@userid", group.creator_id);
            conn.Open();
           int i = com.ExecuteNonQuery();
           if (i >= 1)
           {

               return 1;
           }
           else
           {
               return 0;
           }

This js for posting data:

            save: function () {
            var jForm = $("#form1");
            Metronic.blockUI();
            GroupAPI.create(jForm.serialize(),
                function (data) {
                    console.log(data);
                },
                function (error) {
                    console.log(error);
                },
                function () { Metronic.unblockUI(); });
        }
    }
}();

var GroupAPI = function () {
    var url_create = "api/usergroup/createGroup";
    var url_list = "api/usergroup/list";

    return {
        create: function (item, done, fail, always) {
            var jqxhr = $.post(url_create, item);
            jqXhrHandler(jqxhr, done, fail, always);
        }
    }
}();

Upvotes: 3

Views: 116

Answers (2)

Sean
Sean

Reputation: 3042

it will be better to response a 422 status code, in this case indicate validation failed and server is not able to process the request, you can as well put the user readable message in the response response body

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

Upvotes: 0

Saif AL-Qiari
Saif AL-Qiari

Reputation: 469

Change user group class

UserGroupDalc Dalc = new UserGroupDalc();

public int Create(UserGroup group, int creatorId)
{
 if(ByName(group.name) != null){
 return 1;
 }
 return Dalc.CreateGroup(group, creatorId);
}

js

save: function () {
        var jForm = $("#form1");
        Metronic.blockUI();
        GroupAPI.create(jForm.serialize(),
            function (data) {
                //console.log(data);
           if (data == 0)
            {
               alert('added');
            }else if(data == 1){
               alert('already exist');
            }
            },
            function (error) {
                console.log(error);
            },
            function () { Metronic.unblockUI(); });
    }
   }
 }();

Upvotes: 2

Related Questions