3

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);
        }
    }
}();
2
  • Can you post your code in client side?? Commented Oct 27, 2015 at 4:25
  • I mean the javascript part, how you post the data to server?? Commented Oct 27, 2015 at 4:37

2 Answers 2

2

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(); });
    }
   }
 }();
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.