Pass (Send) parameter to controller on RadioButton selected (checked) in ASP.Net MVC

AliYilmaz
 
on Aug 02, 2021 12:37 AM
577 Views

Hi,

When the radio button on the page is checked, the controller parameter will be sent.

Then, according to the result returned from the method, shipping options will appear.

                <div class="container-fluid" id="kargolar" style="display:none">
                    @foreach (var sm in Model.ShippingMethods)
                    {
                        var width = Convert.ToDouble(Convert.ToDouble(75) / Convert.ToDouble(Model.ShippingMethods.Count)).ToString("###.####").Replace(",", ".");
                        <div class="col-xs-1" style="width:@(width)%; text-align:center;">
                            <img src="@sm.ShippingOptionPicture.ImageUrl" class="shipping-option-image" /><br />
                            <label for="shippingoption-@(sm.ShippingOptionUID)___@(sm.ShippingRateComputationMethodSystemName)">@sm.Name</label><br />
                            <label for="shippingoption-@(sm.ShippingOptionUID)___@(sm.ShippingRateComputationMethodSystemName)">@sm.Description</label><br />
                            <label for="shippingoption-@(sm.ShippingOptionUID)___@(sm.ShippingRateComputationMethodSystemName)">@sm.Rate</label><br />
                            <input type="radio" id="shippingoption-@(sm.ShippingOptionUID)___@(sm.ShippingRateComputationMethodSystemName)" value="@sm.ShippingOptionUID" checked="@sm.Selected" name="shippingoption" onchange="shippingOptionOnChange(this);" />
                        </div>
                    }
                </div>
                <script type="text/javascript">
                    $(function () {
                        var month = $("#hfSelectedShippingAddressId").val();
                        $.ajax({
                            cache: false,
                            type: 'POST',
                            url: '@Url.Action("TscAddress", "TscCheckout", new {ilces =  })',
                            data: postData,
                            dataType: 'json',
                            success: function (data) {
                                location.href = data.redirect;
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert('@T("Plugins.Ltb.ThreeStepCheckout.Checkout.HasWarningOnAddressDelete")');
                            }
                        });
                    });
                </script>

Controller

        public ActionResult TscAddress(FormCollection form, string ilces)
        {
            var cart = _workContext.CurrentCustomer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                .LimitPerStore(_storeContext.CurrentStore.Id)
                .ToList();

       

            var model = new CheckoutAddressModel(Session);
            string ilce = model.ExistingAddress.Select(x => x.DistrictName).FirstOrDefault();            

            if (ilces != null)
            {
                KolayGelsin(ilces);
            }

            

            return View(model);
        }


        private string KolayGelsin(string ilce)
        {
            List<KolayGelsinModel> KolayGelsinModel = new List<KolayGelsinModel>();

            string connectionString = @"server=172.26.100.20;Initial Catalog=preprod.ltbjeans.com;Integrated Security=False;Persist Security Info=False;User ID=nopadmin;Password=1QAsw23edFR$;";

            // SqlConnection sınıfı ile SQL Sunucusuna bağlanma
            System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
            sqlConnection.Open();
            string odemetakip = "select * from KolayGelsin where ILCE=@ilce";

            System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(odemetakip, sqlConnection);
            sqlCommand.Parameters.AddWithValue("ilce", ilce);

            System.Data.SqlClient.SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
          

            while (sqlDataReader.Read())
            {
                KolayGelsinModel.Add(new KolayGelsinModel
                {
                    Id = Convert.ToInt32(sqlDataReader["ID"]),
                    IL = sqlDataReader["IL"].ToString(),
                    ILCE = sqlDataReader["ILCE"].ToString(),                   
                });               
            }

            string ilcead = KolayGelsinModel.Select(x => x.ILCE).FirstOrDefault();
          
            sqlDataReader.Close();
            sqlConnection.Close();

            return ilcead;            
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
AliYilmaz
 
on Aug 04, 2021 04:31 AM

Hi,

I solved this

public JsonResult Sonuc(int? ilces)
{
    return Json(model, JsonRequestBehavior.AllowGet);
}