什么是将ID从编辑模式转换为jQuery函数的最佳方法

时间:2020-08-05 10:22:57

标签: c# jquery asp.net-core

我正在调用一个JavaScript函数,该函数依赖于设置的case ID,此case ID是通过edit函数设置的。显然,我需要某种将大小写ID放入jQuery函数的方法,我在想也许我应该为此解析查询字符串,因为您看到的模式是MISObjects/Edit/5,其中5是大小写ID?

我正在使用asp.net core 3.1和Entity Framework Core。我在组件视图中有此代码,以便可以将其他模型传递给它。

但是,当然,在编辑操作完成之前会调用我的jquery,它们所使用的变量将变为空

// GET: MISObjects/Edit/5
public async Task<IActionResult> Edit(int id) {
if (id == null) {
    return NotFound();
}
        SetupViewBags();      
        var list = _userManager.Users.ToListAsync();
        ViewBag.Users = list;
        var mISObject = await _context.MISobject.FindAsync(id);
        HttpContext.Session.SetString("CaseId", id.ToString());
        //Using temp data to store
        TempData["CaseId"] = id.ToString();
        SetupUploadsViewBags();    
      if (mISObject == null) {
      return NotFound();
 }
     return View(mISObject);
 }

但是,当我调试该值时,其值为空,因为GetAuditTrailData至少在编辑功能触发之前就被调用了。

[HttpGet]
public ActionResult<DataTableWrapper<List<MISAuditTrail>>> GetAuditTrailData() {           
        Int32.TryParse(TempData.Peek["CaseId"], out int resultCaseId);          
        var auditTrailsHistory = _context.MisAuditTrail.Where(w => w.isActive == true && w.isDeleted == false && w.MISObjectId== resultCaseId).ToList();
    string output = JsonConvert.SerializeObject(auditTrailsHistory);
        DataTableWrapper<List<MISAuditTrail>> data = new DataTableWrapper<List<MISAuditTrail>>() {
            data = auditTrailsHistory
        };

        return data;
   }

这是驻留在我的组件视图中的jQuery

@section scripts {
<script>
    
    $(document).ready(function () {
        $("#audTrailTable").DataTable({
            "ajax": {
                url: "/MISObjects/GetAuditTrailData/"  ,
                type: "get",
                database: "json"
            },
            "columns": [
                { "data": "createdDate" },
                { "data": "createdBy" },
                { "data": "action" }
            ],
            "scrollY": "200px",
            "scrollCollapse": true,
            "paging": false,
            "processing": true, // for show progress bar
            "filter": true, // this is for disable filter (search box)
            "orderMulti": false // for disable multiple column at once
        })
    });
    alert(caseId);
</script>

}

我的视图组件

namespace MISSystem.Web.ViewComponents {

 [ViewComponent(Name = "AudiTrailList")]
 public class AuditTrailViewComponent : ViewComponent {
    private readonly IHttpContextAccessor _contextAccessor;

    private readonly MISDBContext db;
    public int CaseId { get; set; }
    public AuditTrailViewComponent(MISDBContext context , IHttpContextAccessor contextAccessor) {
        db = context;
        _contextAccessor = contextAccessor;

    }


    public async Task<IViewComponentResult> InvokeAsync(
 int caseId) {
        CaseId = caseId;

        var items = await GetItemsAsync(caseId);
        
        _contextAccessor.HttpContext.Session.SetString("CaseId",CaseId.ToString());
         return View(items);
    }
    private Task<List<MISAuditTrail>> GetItemsAsync(int caseId) {
        return db.MisAuditTrail.Where(x => x.isActive ==true &&  x.MISObjectId == caseId).ToListAsync();
    }
 }
}

以这种方式调用组件,这在编辑模式触发时很好,它找到了案例的ID,但是当ajax调用thats函数时,它消失了并且为空。

@await Component.InvokeAsync("AudiTrailList", new { caseId = Model.Id })

0 个答案:

没有答案