简体   繁体   English

如何在ajax调用上更新partialview

[英]How to update partialview on ajax call

We are having this situation where we stuck in ajax: 在这种情况下,我们陷入了ajax问题:

This is how we use partial from our view: 从我们的角度来看,这就是我们使用partial的方式:

<div class="MyCheck check-come" id="checklist">
                @if (Model.CurToDo.TrnToDoList != null)
                {<ul>
                    @foreach (var item in Model.CurToDo.TrnToDoList)
                    {
                        <li>@Html.Partial("ToDoChecks", item)</li>
                    }
                </ul>
                }
            </div>

And it is being called using ajax like this: 它使用像这样的ajax来调用:

$("#CurToDo_ToDoTitle").blur(function () {
                    var formdata = new FormData($('#form0').get(0)); // serialize the form
                    formdata.append('ButtonType', 'Save'); // add additional properties
                    $.ajax({
                        url: "/Tasktd/Postit",
                        type: "POST",
                        data: formdata,
                        dataType: "json",
                        processData: false,
                        contentType: false
                    }).success(function (model) {
                        $("#CurToDo_ToDoId").val(model.CurToDo.ToDoId);
                        $("#CurToDo_ToDoTitle").val(model.CurToDo.ToDoTitle);
                        $("#CurToDo_ToDoDesc").val(model.CurToDo.ToDoDesc);
                        $(".panel-header").addClass("show");
                        //$(".check-come").addClass("show");
                        $("#checklist li").html(model);
                        $(".MyCheck").show();
                    });
                });

We would like to have this partial view active as soon as the tasktd is created like as shown in above code of jquery. 我们希望在创建tasktd之后立即激活该局部视图,如上面的jquery代码所示。 But it do nothing. 但是它什么也没做。 At the other end we revisit the page with the same created id as querystring, it loads the partial view. 在另一端,我们使用与querystring相同的创建ID重新访问该页面,它将加载部分视图。

We just want to show the partial view as soon as data is saved for main view. 我们只想在将数据保存为主视图后立即显示局部视图。

And the rendered snippet is this: 呈现的代码段是这样的:

<div class="MyCheck check-come" id="checklist">
            </div>

Note, there is no html generated within it. 请注意,其中没有生成html。

Controller: 控制器:

public ActionResult Index(int tdid = 0)
        {
            ToDoList tdl = new ToDoList();
            ToDo t = new ToDo ();
           // ToDoTrn ttrn = new ToDoTrn();
            List<ToDo> tds = ToDo.Load();
            tdl.ToDos=tds;
            UserView u = new UserView();
            t.CreatedBy = User.Identity.GetUserId();
            u.UserId = User.Identity.GetUserId();
            u.UserName = User.Identity.Name;
           // tdl.tditems.CreatedBy = Convert.ToInt32(userID);
            if (tdid != 0)
            {                
                t = t.LoadToDo(tdid);
                tdl.CurToDo=t;
                ToDoTrn trn = new ToDoTrn(tdid);
                List<ToDoTrn> tlist = new List<ToDoTrn>();
                tlist.Add(trn);
                tdl.CurToDo.TrnToDoList = tlist;
                //tdl.CurToDoTrn = ttrn;
                tdl.UserModel = u;
               // t.UserModel = u;
            }
            else
            {
                tdl.CurToDo=t;
               // tdl.CurToDoTrn = ttrn;
                tdl.UserModel = u;
               // t.UserModel = u;
            }
            return View(tdl);
        }

Saving Action 保存动作

public async Task<ActionResult> Postit(ToDoList td, string ButtonType)
        {
            if (ButtonType == "Delete")
            {
                td.CurToDo.Delete();
                ToDo tt = new ToDo();
                td.CurToDo = tt;
                return Json(new { redirectUrl = Url.Action("Index", "Tasktd"), isRedirect = true });
            }
            else if (ButtonType == "Prioritize")
            {
                td.CurToDo.Prioritize=td.CurToDo.TogglePrioritize();
            }
            else if (ButtonType == "Save")
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                ModelState.Remove("ToDoId");
                ModelState.Remove("ToDoTrnId");
                if (ModelState.IsValid)
                {
                    var t = await td.CurToDo.Save();
                    if (td.CurToDo.TrnToDoList==null)
                    {
                        try
                        {
                            ToDoTrn trn = new ToDoTrn(td.CurToDo.ToDoId);
                            List <ToDoTrn> tlist= new List<ToDoTrn>();
                            tlist.Add(trn);
                            td.CurToDo.TrnToDoList = tlist;
                        }
                        catch(Exception ex)
                        {
                        }

                    }
                   return Json(td, JsonRequestBehavior.AllowGet);
                }
            }
            else if (ButtonType == "CheckSave")
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
               // ModelState.Remove("ToDoId");
                ModelState.Remove("ToDoTrnId");
                if (ModelState.IsValid)
                {
                    //td.CurToDoTrn.ToDoId = td.CurToDo.ToDoId;
                    //var t = await td.CurToDoTrn.ToDoTrnSave(td.CurToDoTrn);
                    return Json(td, JsonRequestBehavior.AllowGet);
                }
            }
            return Json("Oops! Please enter the required values");
        }

Just use a normal ActionResult with an [HttpPost] , initially you can use renderaction in Razer instead of partial. 只需将普通的ActionResult[HttpPost] ,最初就可以在Razer中使用renderaction而不是partial。 Then just do a normal AJAX call and use $("#").html(retVal); 然后只需执行普通的AJAX调用并使用$("#").html(retVal);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM