Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于主视图自定义过滤器更新partia视图_C#_Jquery_Asp.net Core - Fatal编程技术网

C# 基于主视图自定义过滤器更新partia视图

C# 基于主视图自定义过滤器更新partia视图,c#,jquery,asp.net-core,C#,Jquery,Asp.net Core,代码搜索视图和部分结果视图 搜索视图 @model Shared.Model.Search.GLSearch @{ ViewData["Title"] = "Search GL"; } <!-- start page title --> <div class="row"> <div class="col-12"> <div class=&

代码搜索视图和部分结果视图

搜索视图

  @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>
    
控制器:

 [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) });
        }
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务AddOrEditGL(int-id,GLModel-GLModel)
{
if(ModelState.IsValid)
{
var mappedGL=_mapper.Map(glModel);
//插入
如果(id==0)
{                 
wait_glService.CreateGL(mappedGL);
}
//更新
其他的
{
wait_glService.UpdateGL(mappedGL);
//呼叫更新
}
//如何基于主视图窗体控件筛选窗体
返回Json(新的{isValid=true,html=Helper.RenderRazorViewToString(这个,“\u GLViewAll”,null)});
}
返回Json(新的{isValid=false,html=Helper.RenderRazorViewToString(这是“AddOrEditGL”,glModel)});
}
我当前的解决方案再次调用api(2个服务器调用),一个用于更新,另一个用于调用更新表。。我需要在一个电话里做同样的事情。请帮我做什么


注意:我不需要完整的解决方案,我只需要知道如何获取AddOrEditGL Controller post方法主视图表单控件文本fieds文本,以便在DB中进行过滤

我建议使用jQuery低调的AJAX库。它非常容易使用:

它可以获取部分视图,并使用
data ajax update=“#panel”

然后,当模态放置在容器中时,使用
dataajaxsuccess
属性调用回调方法,使用jQuery非干扰性验证解析表单,如下所示:

function SuccessCallback() {
   //You can also use the keyword "this" instead of getting the form with jquery
   $.validator.unobtrusive.parse($(this));
   //or
   $.validator.unobtrusive.parse($form);
}

<div class="modal fade" id="eModal" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog modal-lg">
        <div class="modal-content id="modalContentE">
            <div class="modal-header">
                <h4 class="modal-title"><i class="nav-icon fas fa-edit"></i> Edit Data</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true"><i class="fas fa-times-circle text-white"></i></span>
                </button>
            </div>  
            //New data goes here   
        </div>
    </div>
</div>
非常有用的资源:

更新

我在以前的项目中所做的是包含一个空的编辑模式(一个具有空主体的模式),然后使用库和
数据ajax update
每当用户按下表项上的编辑按钮时,我都会替换该模式的主体,如下所示:

function SuccessCallback() {
   //You can also use the keyword "this" instead of getting the form with jquery
   $.validator.unobtrusive.parse($(this));
   //or
   $.validator.unobtrusive.parse($form);
}

<div class="modal fade" id="eModal" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog modal-lg">
        <div class="modal-content id="modalContentE">
            <div class="modal-header">
                <h4 class="modal-title"><i class="nav-icon fas fa-edit"></i> Edit Data</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true"><i class="fas fa-times-circle text-white"></i></span>
                </button>
            </div>  
            //New data goes here   
        </div>
    </div>
</div>


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

[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) });
}
添加编辑:

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

谢谢…我的添加按钮在主视图内,编辑按钮在表格内(搜索\部分视图),提交后我需要刷新我的搜索_PartiaView@Glen没关系,只需获取库并将属性添加到您的编辑按钮中,如答案中所示,您就可以开始了。我是新的asp.net核心和mvc。您可以为我的scenario@Glen更新了我的答案。感谢使用数据ajax更新它我会更新空模式??你的showAll是什么(4,1)?您似乎想在一个请求中添加/更新筛选器?您是如何筛选的?我建议您可以提供一个可以重现此问题的示例。是的,rena..我必须删除showall(4,1)…我只需要在一个请求中添加/更新筛选器。。目前在post方法中,我必须使用筛选结果更新局部视图。。我已经实现了类似于linked blog的功能。你能分享一下你的showAll jquery吗?不确定它是用来做什么的,所以我不知道如何在一个请求中帮助你实现它们。刚才更新了我所有的代码,,。。请排除分页结果…我只准备好主视图控件文本在Post方法(添加/编辑)中进行过滤…showInPopup方法与文章中相同…ThanksThanks@Rena..我是新的jQuery…我以前已经尝试过你的第一个解决方案…但当返回成功弹出窗口仍然打开时如何关闭。2) 你的第二个解决方案是会话,当我处理会话时。会话是最好的方法还是第一个?感谢通过ajax调用的第一个解决方案。你能给我提供一个示例代码吗?thanksHi@Glen,不确定现在的代码是什么,但是如果你想关闭弹出窗口,请添加
$('#form modal')。modal('hide')到您的成功函数。我认为会话会更好,因为第一个会话有许多重复的代码。在我的情况下,如何处理会话?当您单击编辑或添加按钮时,您将在更新或添加记录后调用AddOrEdit操作,您可以读取会话并返回带有会话数据的url。然后您可以在ajax中的Success函数中重定向到url。
<div class="modal fade" id="eModal" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog modal-lg">
        <div class="modal-content id="modalContentE">
            <div class="modal-header">
                <h4 class="modal-title"><i class="nav-icon fas fa-edit"></i> Edit Data</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true"><i class="fas fa-times-circle text-white"></i></span>
                </button>
            </div>  
            //New data goes here   
        </div>
    </div>
</div>
[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) });
}
 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);
}
 [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) });
 }
success: function (res) {
         if (res.isValid) {
               window.location.href = response.url;  
}
         else
              $('#form-modal .modal-body.p-4').html(res.html);
},