MVC部分视图从ajax成功更新(MVC partial view update from ajax success)

编程入门 行业动态 更新时间:2024-10-14 12:20:19
MVC部分视图从ajax成功更新(MVC partial view update from ajax success)

在视图的主页面上,用户选择一个项目,该选择用数据填充局部视图(首先调用它)。 在该局部视图中,用户还可以选择在前一个下方提供编辑局部视图的项目。 这里可以添加,更新或删除新项目。 问题是:当添加新项目时,应更新第一个局部视图。 我试图实现它的方法是这样的:

function addBottle() { var code = $("#Code").val(); var desc = $("#Description").val(); var id = $("#ProjectId").val(); $.ajax({ url: "@Url.Action("AddBottleType", "Managers")", data: { code: code, desc: desc, id: id }, type: "POST", datatype: "text", success: function (data) { if (typeof data == "undefined") { alert("Something went wrong. Sorry!"); } if (data.Success) { $.alert(data.Data, "Success!"); $.ajax({ url: "@Url.Action("BottleTypes", "Managers")", data: { projectId: id }, type: "GET", datatype: "text", }); } else { $.alert(data.Data, "Warning!"); } } }); };

这为我提供了控制器中的数据,但视图未更新。 可能这不是最好的方法,我打开建议,解决方案。 我也做了我的部分,google和stackoverflow是我的朋友,但没有一个解决方案有效。

On the main page in the view the user selects a project and that selection fills up a partial view (call it first partial) with data. In this partial view the user can also select an item which provides an editing partial view under below the previous one. Here a new item can be added, updated or deleted. The question is: when a new item added the first partial view should be updated. The method how I am trying to achieve it is like this:

function addBottle() { var code = $("#Code").val(); var desc = $("#Description").val(); var id = $("#ProjectId").val(); $.ajax({ url: "@Url.Action("AddBottleType", "Managers")", data: { code: code, desc: desc, id: id }, type: "POST", datatype: "text", success: function (data) { if (typeof data == "undefined") { alert("Something went wrong. Sorry!"); } if (data.Success) { $.alert(data.Data, "Success!"); $.ajax({ url: "@Url.Action("BottleTypes", "Managers")", data: { projectId: id }, type: "GET", datatype: "text", }); } else { $.alert(data.Data, "Warning!"); } } }); };

This gives me the data in the controller, but the view is not updated. Probably this is not the best way, I am open the suggestions, solutions. I've also done my part, google and stackoverflow is my friend, but none of the solutions worked.

最满意答案

你需要将它加载到你的div中。 像这样的东西。 检查控制器操作是否成功,因此您无需在前端执行Success():

function addBottle() { var code = $("#Code").val(); var desc = $("#Description").val(); var id = $("#ProjectId").val(); $.ajax({ url: "@Url.Action("AddBottleType", "Managers")", data: { code: code, desc: desc, id: id }, type: "POST", datatype: "text", success: function (data) { if (typeof data == "undefined") { alert("Something went wrong. Sorry!"); } if (data.Success) { $.alert(data.Data, "Success!"); $("#bottleTypes").load("/Managers/BottleTypes", { projectId: id }); } else { $.alert(data.Data, "Warning!"); } } }); };

You need to load it into your div. Something like this. Check for success in your controller action so you need not do Success() in the front end:

function addBottle() { var code = $("#Code").val(); var desc = $("#Description").val(); var id = $("#ProjectId").val(); $.ajax({ url: "@Url.Action("AddBottleType", "Managers")", data: { code: code, desc: desc, id: id }, type: "POST", datatype: "text", success: function (data) { if (typeof data == "undefined") { alert("Something went wrong. Sorry!"); } if (data.Success) { $.alert(data.Data, "Success!"); $("#bottleTypes").load("/Managers/BottleTypes", { projectId: id }); } else { $.alert(data.Data, "Warning!"); } } }); };

更多推荐

data,view,视图,局部,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-05 19:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   ajax   MVC   partial   success

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!