C# Ajax.begin刷新部分。。?
好的,我有一个使用ajax发布的表单C# Ajax.begin刷新部分。。?,c#,asp.net-mvc,C#,Asp.net Mvc,好的,我有一个使用ajax发布的表单 <% using(Ajax.BeginForm(new AjaxOptions() { OnBegin="onBegin", OnSuccess = "onSuccess", OnFailure="onFailure" })) { %> 在服务器端,我从控制器传回一个Json对象。现在,当OnSuccess事件触发时,我可以使用“result.get_response().get_object()”来访问Json对象 我的问题是,我需要能
<% using(Ajax.BeginForm(new AjaxOptions() { OnBegin="onBegin", OnSuccess = "onSuccess", OnFailure="onFailure" })) { %>
在服务器端,我从控制器传回一个Json对象。现在,当OnSuccess事件触发时,我可以使用“result.get_response().get_object()”来访问Json对象
我的问题是,我需要能够用Json对象中的项目列表刷新页面上的部分
关于如何做到这一点的想法?为要显示的项目列表创建用户控件,并通过将json数据传递给UC将其呈现为部分。这将使用jQuery部分刷新,并旋转返回的JSON对象,构建任何您喜欢的内容 例如:
$.each(json, function(i, item) {
//Add a dinner to the list on the right
$('#dinnerList').append($('<li/>')
.attr("class", "dinnerItem")
.append($('<a/>').attr("href", "/Dinners/Details/" + item.ID)
.html(item.Name)).append("SomeThing"));
});
$。每个(json,函数(i,项){
//将晚餐添加到右侧的列表中
$(“#晚餐列表”)。追加($(“”)
.attr(“班级”、“餐厅”)
.append($('').attr(“href”,“/dinters/Details/”+item.ID)
.html(item.Name)).append(“某物”);
});
我想下面的代码会对您有所帮助
首先,使用
- RefreshAjaxList:当前控制器的名称ajax操作
- string.empty(可选)
- AJAX选项
- 表单id(可选)
- 单击状态时,我们将编辑状态呼叫服务器以更新状态
- 编辑状态后,我们调用submit按钮调用RefreshAjaxList;按钮显示为:无
- 在本例中,我有一个控制器:
,有两个动作:AjaxController
public ActionResult UpdateStatus(int contactId,Status contactStatus)
{
ContactRepository repo=新建ContactRepository();
报告更新状态(联系人ID、联系人状态);
返回Json(“success:true”);
}
[接受动词(HttpVerbs.Post)]
[ActionName(“RefreshAjaxList”)]
公共行动结果刷新联系人()
{
ContactRepository repo=新建ContactRepository();
IList list=repo.list();
返回PartialView(“AjaxUc/AjaxList”,repo.List());
}
JavaScript:
var status = { active: 1, inactive: 0 };
function editStatus(cell, id, active) {
if (active)
cell.innerHTML = "<input type='radio' checked='true' name='active" + id + "' onclick='updateStatus(this, " + id + ", true);'>Active</input>" +
"<input type='radio' name='active" + id + "' onclick='updateStatus(this, " + id + ", false);'>Inactive</input>";
else
cell.innerHTML = "<input type='radio' name='active" + id + "' onclick='updateStatus(this, " + id + ", true);'>Active</input>" +
"<input type='radio' checked='false' name='active" + id + "' onclick='updateStatus(this, " + id + ", false);'>Inactive</input>";
}
function updateStatus(radio, id, active) {
if (radio.checked != active) {
if (confirm("Do you want to change the status of the contract?")) {
if (active)
cStatus = status.active;
else
cStatus = status.inactive;
$.ajax({
url: 'Ajax/UpdateStatus',
dataType: "json",
data: { contactId: id, contactStatus: cStatus },
success: function(html) {
jQuery("#divAjaxList").submit();
},
error: function(request, desc, ex) {
alert(desc);
}
});
}
}
}
var status={active:1,inactive:0};
功能编辑状态(单元格、id、活动){
如果(活动)
cell.innerHTML=“活动”+
“不活跃”;
其他的
cell.innerHTML=“活动”+
“不活跃”;
}
功能更新状态(收音机、id、活动){
如果(radio.checked!=活动){
如果(确认(“是否要更改合同状态?”){
如果(活动)
cStatus=status.active;
其他的
cStatus=status.inactive;
$.ajax({
url:'Ajax/UpdateStatus',
数据类型:“json”,
数据:{contactId:id,contactStatus:cStatus},
成功:函数(html){
jQuery(“#divAjaxList”).submit();
},
错误:函数(请求、描述、ex){
警报(desc);
}
});
}
}
}
您的ASP脚本:
<% using (Ajax.BeginForm("RefreshAjaxList", "abc", new AjaxOptions() { UpdateTargetId = "divContent" }, new { @id = "divAjaxList" }))
{%>
<div id="divContent">
<table>
<tr>
<th>
</th>
<th>
Id
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Phone
</th>
<th>
Email
</th>
<th>
Status
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id = item.Id })%>
|
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= Html.Encode(item.Id)%>
</td>
<td>
<%= Html.Encode(item.FirstName)%>
</td>
<td>
<%= Html.Encode(item.LastName)%>
</td>
<td>
<%= Html.Encode(item.Phone)%>
</td>
<td>
<%= Html.Encode(item.Email)%>
</td>
<td ondblclick="editStatus(this,<%=item.Id %>, <%= item.Status.GetHashCode() %>);">
<%= item.Status== Status.Active ? "Active" :"Inactive" %>
</td>
</tr>
<% } %>
</table>
<input type="submit" id="refresh" style="display:none" />
</div>
<% } %>
身份证件
名字
姓氏
电话
电子邮件
地位
|
欲了解更多信息,请发送至pnguyen2@firstlook.com供进一步讨论
希望这能对你有所帮助。你也可以用它来呈现JSON数据你能给我举个例子吗。我不确定在提交表单后如何将json数据传递给UC。。。
<% using (Ajax.BeginForm("RefreshAjaxList", "abc", new AjaxOptions() { UpdateTargetId = "divContent" }, new { @id = "divAjaxList" }))
{%>
<div id="divContent">
<table>
<tr>
<th>
</th>
<th>
Id
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Phone
</th>
<th>
Email
</th>
<th>
Status
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id = item.Id })%>
|
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= Html.Encode(item.Id)%>
</td>
<td>
<%= Html.Encode(item.FirstName)%>
</td>
<td>
<%= Html.Encode(item.LastName)%>
</td>
<td>
<%= Html.Encode(item.Phone)%>
</td>
<td>
<%= Html.Encode(item.Email)%>
</td>
<td ondblclick="editStatus(this,<%=item.Id %>, <%= item.Status.GetHashCode() %>);">
<%= item.Status== Status.Active ? "Active" :"Inactive" %>
</td>
</tr>
<% } %>
</table>
<input type="submit" id="refresh" style="display:none" />
</div>
<% } %>