Monday, 14 September 2020

Update the partia view based on Main view custom filters

Code searchView and PartialResultView

SearchView

<form asp-action="" asp-controller="" method="post">


    <div class="row">
        <div class="col-lg-12">
            <div class="card-box">
                <div class="form-row">

                    
                    <div class="form-group col-md-2">
                        <label asp-for="Name" class="col-form-label"></label>
                        <input asp-for="Name" type="text" class="form-control" />

                    </div>
                    <div class="form-group col-md-2">
                        <label asp-for="Code" class="col-form-label"></label>
                        <input asp-for="Code" type="text" class="form-control" />

                    </div>
                    <div class="form-group col-md-3">
                        <label asp-for="GLSectionId" class="col-form-label">Section </label>
                        <select asp-for="GLSectionId" asp-items="@(new SelectList(Model.glSections,"Id","Name"))" class="form-control">
                            <option value="">Choose</option>

                        </select>
                    </div>
                    <div class="form-group col-md-3">
                        <label asp-for="GLGroupId" class="col-form-label">Group</label>
                        <select asp-for="GLGroupId" asp-items="@(new SelectList(Model.glGroups,"Id","Name"))" class="form-control">
                            <option value="">Choose</option>

                        </select>
                    </div>
                    <button type="button" id="glsearch" class="btn btn-primary waves-effect waves-light">Search</button>

                </div>



            </div> <!-- end card-box -->


        </div> <!-- end col -->

    </div> <!-- end row -->



</form>

<button type="button" class="btn btn-success waves-effect waves-light" data-toggle="modal" data-target="#con-close-modal">Create New</button>


<div id="partialresult"></div>

<partial name="_AddEditGL" />


@section scripts
    {

    <script type="text/javascript">


            let pagesize =  4;
            let pageindex = 1;


            document.addEventListener('DOMContentLoaded', function () {
                getgl(pagesize, pageindex);   
            });

            ...other code populate the grid
            </Script>
            
}

Search_PartiaView

   @model PagedResult<Shared.Model.Masters.GLMaster.GLViewModel>
    @{
    
    }
    
    
    @if (Model == null || Model.RowCount == 0)
    {
        <p>No results found</p>
    }
    else
    {
    
        <div class="col-lg-12">
            <div class="card-box">
                <h4 class="header-title">Customers</h4>
                <p class="sub-header">
    
                </p>
    
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th data-priority="1">#</th>
                                <th data-priority="3">Name</th>
                                <th data-priority="6">Code</th>
                                <th data-priority="6">Section</th>
                                <th data-priority="6">Group</th>
                                <th data-priority="6">Action Links</th>
    
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Results)
                            {
                            <tr>
                                <th scope="row">@item.Id</th>
                                <td>@item.Name</td>
                                <td>@item.Code</td>
                                <td>@item.GLSection</td>
                                <td>@item.GLGroup</td>
                                <td>
                                    <a asp-controller="Customer" asp-action="Details" asp-route-id="@item.Id">Details</a> |
                                    <a asp-controller="Customer" asp-action="EditCustomer" asp-route-id="@item.Id">Edit</a> |
                                    <a asp-controller="Customer" asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                                </td>
    
                            </tr>
    
                            }
    
                        </tbody>
                    </table>
                </div> <!-- end table-responsive-->
    
            </div> <!-- end card-box -->
        </div> <!-- end col -->
        <!-- Responsive Table js -->
    
    
    
    }

Partial View (AddEditGL)

 <div id="con-close-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Add New GL</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                </div>
                <div class="modal-body p-4">
                    <form>

                      ...
                      
                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-info waves-effect waves-light">Create</button>
                </div>
            </div>
        </div>
    </div>

I have View with Partial view (is for results in table) . When i click Edit button in Search_PartiaView i need to open popup (Partial View (AddEditGL)) and data should be loaded ajax and submit the button after update.. I need to use jquery unobtrusive validation in popup and also without refresh the page ..Please let me know hw to do..Thanks

EDIT

I Have implemented similar to this Ajax crud popup

I Have Main view and Partial view. Also AddOrEdit View for Add/edit Master.

My Current solution works.. But inmy main view i have filter based on 2 filds.

After add/edit grid load all the result but if filter applied i also need to filter the grid ..

My Javascript code Here:

jQueryAjaxPost = form => {
    try {
        $.ajax({
            type: 'POST',
            url: form.action,
            data: new FormData(form),
            contentType: false,
            processData: false,
            success: function (res) {
                if (res.isValid) {
                    $('#view-all').html(res.html)    --- here actually data coming all without filter                
                    $('#form-modal .modal-body.p-4').html('');
                    $('#form-modal .modal-title').html('');
                    $('#form-modal').modal('hide');
                    showAll(4, 1);   ---  it is the javascript fuction call  to call the
 api again
                }
                else
                    $('#form-modal .modal-body.p-4').html(res.html);
            },
            error: function (err) {
                console.log(err)
            }
        })
        //to prevent default form submit event
        return false;
    } catch (ex) {
        console.log(ex)
    }
}

Controller:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> AddOrEditGL(int id,GLModel glModel)
        {
            if (ModelState.IsValid)
            {

                var mappedGL = _mapper.Map<GLDTO>(glModel);
                //Insert
                if (id == 0)
                {                 
                    await _glService.CreateGL(mappedGL);                  
                }
                //Update
                else
                {
                    await _glService.UpdateGL(mappedGL);
                    //Call Update
                }

               // How do i filter  the based on Main view  form controls
                  
                return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_GLViewAll", null) });
            }
            return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEditGL", glModel) });
        }

my Current solution call the api again ( 2 server calls) one for update and another for call update table .. i need to do the same in single call ..Please help to do?

Note: I dont need complete solution , I only need to how to get the AddOrEditGL Controller post method Main view form control text fieds text to filter in DB



from Update the partia view based on Main view custom filters

No comments:

Post a Comment