[RESOLVED]Multiple Submit button

Hi Friends I have two buttons in a singe form and i want to Target two different methods but not able to achieve that .

For ex. my for is having two button Save and Add
in my edit view

so when i click save button i want to execute save action method and when i click Add then i want to execute Add action method.

here is my controller code 

        [ActionName("Edit")]       
        [HttpPost]              
        public ActionResult Edit([Bind(Include = "ID,CONTRACTCODE,DESCRIPTION,CONTRACTSTATUS,CONCESSIONAIREID,SALESREPRESENTATIVE,APPROVALSTATUS,CONTRACTPERIOD,CONTRACTSTARTDATE,CONTRACTENDDATE,TOTALAREA,ISRENEWEDCONTRACT,ISTRANSFERREDCONTRACT,NOTICEPERIOD,NOTICEDATE,NOTICEEXPIRYDATE,FITOUTSTARTDATE,FITOUTENDDATE,NOOFRENTALOBJECTS,TOTALMINGUARANTEDAMT,STOREOPENINGDATE,PARTITIONID,COMPANYID,SITEID,CREATEDDATE,CREATEDBY,MODIFIEDBY,MODIFIEDDATE,REVENUESHAREONTAX,REVENUESHAREONDISCOUNT")] CONTRACTTABLE cONTRACTTABLE)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    db.Entry(cONTRACTTABLE).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(cONTRACTTABLE);
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
                throw;
            }

        }

        [ActionName("Edit")]       
        [HttpPost]       
        public ActionResult Add([Bind(Include = "ID,CONTRACTCODE,DESCRIPTION,CONTRACTSTATUS,CONCESSIONAIREID,SALESREPRESENTATIVE,APPROVALSTATUS,CONTRACTPERIOD,CONTRACTSTARTDATE,CONTRACTENDDATE,NOTICEPERIOD,NOTICEDATE,NOTICEEXPIRYDATE,FITOUTSTARTDATE,FITOUTENDDATE,NOOFRENTALOBJECTS,TOTALMINGUARANTEDAMT,STOREOPENINGDATE")] CONTRACTTABLE cONTRACTTABLE)
        {
            Session["CT"] = cONTRACTTABLE;
            Session["CROT"] = (from row in db.CONTRACTRENTALOBJECTTABLEs
                               where row.CONTRACTID == cONTRACTTABLE.ID
                               select row).ToList();

            return RedirectToAction("Create", "CONTRACTRENTALOBJECTTABLEs");
        }

yogeshweb

n i click save button i want to execute save action method and when i click Add then i want to execute Add action method.

change FOrm action with javascript

Hi yogeshweb,

Thanks for your post.

You can use javascript to implement it as community member mentioned.

Please check this reference:

#ASP.NET MVC: Using ActionMethodSelectorAttribute to Handle Multiple Submit Buttons

http://www.dotnetcurry.com/showarticle.aspx?ID=724

I think it will help you.

Best Regards,

Eileen

With an ajax call since the form you can redirect to the function you want when a button is clicked. Also you can do it with an unique ajax call and passing a parameter showing what you want do, for example a boolean called isSave. Inside of the controller
you can do different things depending this value.

        $("#btSave").click(function () {

                var params = {
                  /* your vars here */
                };
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("YourfunctionName", "YourControllerName")',
                    data: params,
                    dataType: 'json',
                    success: function (data) {
                    }
                });
        });

        $("#btAdd").click(function () {

                var params = {
                  /* your vars here */
                };
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("YourfunctionName", "YourControllerName")',
                    data: params,
                    dataType: 'json',
                    success: function (data) {
                    }
                });
        });

Leave a Reply