根据主视图自定义过滤器更新partia视图 [英] Update the partia view based on Main view custom filters

查看:61
本文介绍了根据主视图自定义过滤器更新partia视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码searchView和PartialResultView

Code searchView and PartialResultView

SearchView

  @model Shared.Model.Search.GLSearch
@{

    ViewData["Title"] = "Search GL";
}
<!-- start page title -->
<div class="row">
    <div class="col-12">
        <div class="page-title-box">
            <div class="page-title-right">
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"><a href="javascript: void(0);">UBold</a></li>
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Forms</a></li>
                    <li class="breadcrumb-item active">Elements</li>
                </ol>
            </div>
            <h4 class="page-title">Search Customer</h4>

        </div>
    </div>
</div>
<!-- end page title -->



<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="search" class="btn btn-primary waves-effect waves-light">Search</button>

                </div>



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


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

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



</form>

<div id="view-all"></div>
    

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">
                                <a onclick="showInPopup('@Url.Action("AddOrEditGL","GLMaster",new {area = "Masters"},Context.Request.Scheme)','New GL')" class="btn btn-success text-white"><i class="fas fa-random"></i> New GL</a>
                            </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>
                                <div>
                                    <a onclick="showInPopup('@Url.Action("AddOrEditGL","GLMaster",new { area= "Masters",id = item.Id},Context.Request.Scheme)','Update GL')" class="btn btn-info text-white"><i class="fas fa-pencil-alt"></i> Edit</a>
                                    <form  asp-area="Masters"  asp-action="DeleteGL" asp-route-id="@item.Id" onsubmit="return jQueryAjaxDelete(this)" class="d-inline">
                                        <input type="submit" value="Delete" class="btn btn-danger" />
                                    </form>
                                </div>
                            </td>

                        </tr>

                        }

                    </tbody>
                </table>
            </div> <!-- end table-responsive-->

        </div> <!-- end card-box -->
    </div> <!-- end col -->
    <!-- Responsive Table js -->



}

部分视图(AddEditGL)

@model Shared.Model.Masters.GLMaster.GLModel
@{
    Layout = null;
    ViewData["Title"] = "Add Customer";
}

<div class="row">
    <div class="col-lg-12">
        <div class="card-box">
            <form asp-action="AddOrEditGL" asp-controller="GLMaster" asp-area="Masters" asp-route-id="@Model.Id" onsubmit="return jQueryAjaxPost(this);">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <input type="hidden" asp-for="@Model.Id" />
              

                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label asp-for="Name" class="control-label"></label>
                            <input asp-for="Name" type="text" class="form-control">
                            <span asp-validation-for="Name" class="text-danger"></span>
                        </div>
                    </div>

                    <div class="col-md-4">
                        <div class="form-group">
                            <label asp-for="NameLang" class="control-label"></label>
                            <input asp-for="NameLang" type="text" class="form-control">
                            <span asp-validation-for="NameLang" class="text-danger"></span>
                        </div>
                    </div>


                </div>

           

                <div class="form-group">
                    <div class="col-md-6 offset-md-3">
                        <input type="submit" value="Submit" class="btn btn-primary btn-block" />
                    </div>
                </div>


            </form>

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


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

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

我具有部分视图视图(用于表中的结果).当我在 Search_PartiaView 中单击编辑"按钮时我需要打开弹出窗口(部分视图(AddEditGL))和数据应加载ajax并在更新后提交按钮..我需要在弹出窗口中使用jquery非侵入式验证,并且也无需刷新页面..请让我知道如何做..谢谢

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

编辑

我已经实现了类似于此

I Have implemented similar to this Ajax crud popup

我有主视图和部分视图.还可以使用AddOrEdit视图来添加/编辑母版.

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

我当前的解决方案有效..但是在我的主视图中,我有基于2个过滤器的过滤器.

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 ..

我的Java代码在这里:

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)
    }
}

控制器:

 [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) });
        }

我当前的解决方案再次调用api(2个服务器调用),一个用于更新,另一个用于调用更新表..我需要在单个调用中执行相同操作..请帮忙吗?

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?

注意:我不需要完整的解决方案,只需要获取AddOrEditGL Controller发布方法主视图表单控件文本即可将文本过滤到数据库中

推荐答案

如果要在一个请求中更新/添加并显示搜索到的数据,快速的方法是将 SearchGLPartial 代码复制到 AddOrEditGL 函数,并通过ajax传递pageSize,pageIndex,name,code,GLSectionId和GlGroupId:

If you want to update/add and show the searched data in one request,The quick way is to copy the SearchGLPartial code to the AddOrEditGL function and pass the pageSize,pageIndex,name,code,GLSectionId and GlGroupId by ajax:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddOrEditGL(int id,GLModel glModel,string name,string code,string GLSectionId,string GlGroupId...)
{
    if (ModelState.IsValid)
    {
        var mappedGL = _mapper.Map<GLDTO>(glModel);
        //Insert or
        //Update
        //copy the SearchGLPartial code here and return view with data
    }
    return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEditGL", glModel) });
}

如果您只是不想删除showViewAll()jQuery,我认为您可以在首次在 SearchGLPartial 操作中搜索数据时为过滤条件设置会话.然后在AddOrEdit操作中,获取会话并设置正确的URL.最后,您可以在ajax成功响应中获得该URL:

If you just do not want to remove showViewAll() jquery,I think you could set session for the filter condition when you first search the data in SearchGLPartial action.Then in your AddOrEdit action,get the session and set the correct url.Finally,you could get the url in ajax success response:

 public IActionResult SearchGLPartial(string name,string code,string GLSectionId,string GLGroupId)
{
    HttpContext.Session.SetString("name", name);
    HttpContext.Session.SetString("code", code);
    HttpContext.Session.SetString("GLSectionId", GLSectionId);
    HttpContext.Session.SetString("GLGroupId", GLGroupId);
    var data = Gllist.Where(a => a.Name.Contains(name) & a.Code.Contains(code)).FirstOrDefault();//filter by yourself
    return PartialView("_Search", data);
}

AddOrEdit:

AddOr

 [HttpPost]
 [ValidateAntiForgeryToken]
 public async Task<IActionResult> AddOrEditGL(int id,GLModel glModel)
 {
        if (ModelState.IsValid)
        {
            //Insert
            //Update
            var name = HttpContext.Session.GetString("name");
            //other session data...
            return Json(new { isValid = true, url="Home/Index?name="+name+"&code="+xxx+"&...."});
        }
        return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEditGL", glModel) });
 }

然后您的ajax:

success: function (res) {
         if (res.isValid) {
               window.location.href = response.url;  
}
         else
              $('#form-modal .modal-body.p-4').html(res.html);
},

这篇关于根据主视图自定义过滤器更新partia视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆