如何将json对象从jquery传递到mvc视图
目前,我有一个列表视图中显示的所有待办事项列表项的列表。我还为列表中的每一行放置了一个编辑超链接。当我单击Edit时,我正在使用jquery异步调用一个Edit操作,并希望在列表视图页面的底部显示带有选中项的Edit视图 下面是List.cshtml的代码-如何将json对象从jquery传递到mvc视图,jquery,json,asp.net-mvc-4,Jquery,Json,Asp.net Mvc 4,目前,我有一个列表视图中显示的所有待办事项列表项的列表。我还为列表中的每一行放置了一个编辑超链接。当我单击Edit时,我正在使用jquery异步调用一个Edit操作,并希望在列表视图页面的底部显示带有选中项的Edit视图 下面是List.cshtml的代码- @using MvcToDoListItemsDemo.ViewModels @model TodoListViewModel <table> <thead> <tr> <
@using MvcToDoListItemsDemo.ViewModels
@model TodoListViewModel
<table>
<thead>
<tr>
<th></th>
<th></th>
<th>TodoItem</th>
<th>Is Completed?</th>
<th>Target Date</th>
<th>Created Date</th>
<th>Completed Date</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.TodoListItems)
{
<tr>
<td>@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "edit", @id=item.Id })</td>
<td>@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class="delete", @id=item.Id })</td>
<td>@item.ActivityDescription </td>
<td>@item.IsCompleted </td>
<td>@item.ActivityDeadLine </td>
<td>@item.CreatedDate </td>
<td>@item.CompletionDate </td>
</tr>
}
</tbody>
</table>
}
下面是Edit.cshtml部分视图
<h2>Edit</h2>
<fieldset>
<legend>TodoListModel</legend>
@Html.LabelFor(model => model.ActivityDescription)
@Html.EditorFor(model => model.ActivityDescription)
@Html.LabelFor(model => model.IsCompleted)
@Html.CheckBoxFor(model => model.IsCompleted)
@Html.LabelFor(model => model.ActivityDeadLine)
@Html.EditorFor(model => model.ActivityDeadLine)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
编辑
TodoListModel
@LabelFor(model=>model.ActivityDescription)
@EditorFor(model=>model.ActivityDescription)
@LabelFor(model=>model.IsCompleted)
@CheckBoxFor(model=>model.IsCompleted)
@Html.LabelFor(model=>model.ActivityDaildate)
@Html.EditorFor(model=>model.ActivityDeadLine)
所以我的问题是-我们如何将异步调用[HttpGet]编辑操作得到的Json结果传递到Edit.cshtml
请告知。有可能吗?我已经用下面的代码解决了这个问题
@Ajax.ActionLink("Edit", "Edit", "TodoList", new { Id = item.Id }, new AjaxOptions
{
UpdateTargetId = "EditView",
HttpMethod = "Get"
})
and return PartialView("Item"); from the controller action.
真的不清楚如何处理响应数据您的ajax请求将JSON返回到
$.ajax
回调,这很好。因此,如果您设法调用Edit.cshtml,那么您希望如何处理生成的HTML?您可能希望忘记JSON,而执行类似于returnpartialview(“Edit”,Item)的操作
因此,您可以在ajax响应中获得HTML,并使用它来填充div
或其他内容。如果我们在javascript函数中硬编码html,它就不能在我的项目中的任何其他地方重用。那么,有没有可能将JSON结果转换为C#Model,并将Model对象传递给Jquery本身的编辑操作呢?有人能回答我的问题吗?你误解了ajax。Ajax调用保持在同一页面上。通常使用链接重定向,而不是使用ajax,并在Edit
方法中返回视图而不是JSONif(Item==null){返回新的HttpNotFoundResult();}否则{返回视图(Item);}
<h2>Edit</h2>
<fieldset>
<legend>TodoListModel</legend>
@Html.LabelFor(model => model.ActivityDescription)
@Html.EditorFor(model => model.ActivityDescription)
@Html.LabelFor(model => model.IsCompleted)
@Html.CheckBoxFor(model => model.IsCompleted)
@Html.LabelFor(model => model.ActivityDeadLine)
@Html.EditorFor(model => model.ActivityDeadLine)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
@Ajax.ActionLink("Edit", "Edit", "TodoList", new { Id = item.Id }, new AjaxOptions
{
UpdateTargetId = "EditView",
HttpMethod = "Get"
})
and return PartialView("Item"); from the controller action.