1 Replies - 146 Views - Last Post: 16 October 2019 - 07:40 AM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 226
  • Joined: 01-July 12

C# MVC Modal with JSON object UPDATE request

Posted 16 October 2019 - 07:33 AM

The data-tst gets the correct ID of the data needed.

I pass the ID to the controller action and i get the object and all the data from the database.

I pass it back to my .ajax() and the data is correct in the .ajax success part of the function.

Now in this part i need to populate the controls (text boxes) and open the modal populated
/*HTML button click (Opens Modal)*/
<input type="button" class="btn btn-danger openModal" data-toggle="modal" data-target="#modalInnerSQ" data-tst="@Model.ListQuotes[i].QuoteID" value="ClickMe" />

/*HTML Modal*/
<div class="modal fade" id="modalInnerSQ" tabindex="-1" role="dialog" aria-labelledby="lblSQmodal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2 class="modal-title">Service Quote Details</h2>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <h3>Service Quote</h3>
                <form>
                    <div class="form-group">
                        @Html.LabelFor(x => x.ServiceVM.CustomerName, htmlAttributes: new { @class = "control-label col-3" })
                        <div class="col-9">
                            @Html.TextBoxFor(x => x.ServiceVM.CustomerName, new { @class = "form-control" })
                            @Html.ValidationMessageFor(x => x.ServiceVM.CustomerName, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(x => x.ServiceVM.MachineCount, htmlAttributes: new { @class = "control-label col-3" })
                        <div class="col-9">
                            @Html.TextBoxFor(x => x.ServiceVM.MachineCount, new { @class = "form-control" })
                            @Html.ValidationMessageFor(x => x.ServiceVM.MachineCount, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(x => x.ServiceVM.MachinePrice, htmlAttributes: new { @class = "control-label col-3" })
                        <div class="col-9">
                            @Html.TextBoxFor(x => x.ServiceVM.MachinePrice, new { @class = "form-control" })
                            @Html.ValidationMessageFor(x => x.ServiceVM.MachinePrice, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(x => x.ServiceVM.MachineAddress, htmlAttributes: new { @class = "control-label col-3" })
                        <div class="col-9">
                            @Html.TextBoxFor(x => x.ServiceVM.MachineAddress, new { @class = "form-control" })
                            @Html.ValidationMessageFor(x => x.ServiceVM.MachineAddress, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(x => x.ServiceVM.MachineCity, htmlAttributes: new { @class = "control-label col-3" })
                        <div class="col-9">
                            @Html.TextBoxFor(x => x.ServiceVM.MachineCity, new { @class = "form-control" })
                            @Html.ValidationMessageFor(x => x.ServiceVM.MachineCity, "", new { @class = "text-danger" })
                        </div>
                    </div>
<div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                @* SAVE BUTTON  [ <button} or form <input> ]*@

            </div>
        </div>
    </div>

/* Javascript and .ajax() */
 $('.openModal').on('click', function () {
        var quoteid = $(this).attr("data-tst");
        $.ajax({
            type: "POST",
            url: '/Service/GetServiceQuote/',
            contentType: "application/json; charset=utf-8",
            data: { "id": quoteid },
            datatype: "json",
            success: function (data) {
                alert('in success');
                var cnam = data.CustomerName;
                alert(cnam);
                //alert(data.CustomerName);
                //$('#ServiceVM_CustomerName').val(data.CustomerName);
                $('#ServiceVM_MachineCity').html(cnam);
                $("ServiceVM_MachineCity ").html(data.MachineCity);
                //$("#ServiceVM_MachineState").innerHTML(data.MachineState);
                document.getElementsByName("ServiceVM_MachineZip").value = data.MachineZip;
                //$('#modalInnerSQ').modal('show');
            }
        })
    })

/* Controller Action */
[HttpPost]
        public JsonResult GetServiceQuote(int? id)
        {
            if (id == null) { return null; }
                
            var vm = new ServiceReportViewModel();
            var model = context.Serv_Quotes.Where(x => x.QuoteID == id).FirstOrDefault();
            if(model.EmployeeID != null)
            {
                var employee = context.Employee_Main.Where(z => z.EmployeeID == model.EmployeeID).FirstOrDefault();
                vm.EmpID = employee.EmployeeID.ToString();
                vm.EmpFirstName = employee.FirstName;
                vm.EmpLastName = employee.LastName;
                vm.EmpPosition = employee.Position;
                vm.EmpEmail = employee.Email;
            }
            vm.QuoteID = model.QuoteID;
            vm.CustomerID = model.CustomerID.ToString();
            vm.CustomerName = model.CustomerName;
            vm.MachineCount = model.MachineCount;
            vm.MachinePrice = model.PricePerMachine;
            vm.MachineAddress = model.Address;
            vm.MachineCity = model.City;
            vm.MachineState = model.State;
            vm.MachineZipCode = model.ZipCode;
            vm.MachineCountry = model.Country;
            vm.ExpirationDate = model.ExpirationDate;
            vm.ListOfSN2s = model.ListOfSN2s;
            vm.ContactID = model.ContactID.ToString();
            vm.ContactName = model.ContactName;
            vm.ContactPhone = model.ContactPhone;
            vm.ContactEmail = model.ContactEmail;
            
            return Json(vm, JsonRequestbehavior.AllowGet);
        }



Is This A Good Question/Topic? 0
  • +

Replies To: C# MVC Modal with JSON object UPDATE request

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,271
  • Joined: 12-December 12

Re: C# MVC Modal with JSON object UPDATE request

Posted 16 October 2019 - 07:40 AM

What?
You have not provided a question nor a clear description.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1