C# 在MVC的部分视图中重定向
我有一个奇怪的场景。我正在使用ASP.NET MVC框架创建一个站点,其中包含一个配置文件页面,该页面包含用户的图片、信息等。我有一个名为profile的视图,该视图使用Ajax操作链接将部分视图加载到div中。以下是一个示例:C# 在MVC的部分视图中重定向,c#,asp.net,.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,.net,Asp.net Mvc,Asp.net Mvc 4,我有一个奇怪的场景。我正在使用ASP.NET MVC框架创建一个站点,其中包含一个配置文件页面,该页面包含用户的图片、信息等。我有一个名为profile的视图,该视图使用Ajax操作链接将部分视图加载到div中。以下是一个示例: @Ajax.ActionLink("Basic Info", "Index", "BasicInfo", new {id=Model.UserName},new AjaxOptions { InsertionMode = Ins
@Ajax.ActionLink("Basic Info", "Index", "BasicInfo",
new {id=Model.UserName},new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "content",
HttpMethod = "GET"
})
BasicInfo的索引操作只显示用户的基本信息。我希望在该局部视图中有一个编辑链接,当按下该链接时,加载另一个动作方法,编辑我可以编辑的值(另一个局部视图)。我有几个问题:
1) 我没有将概要文件设置为布局,即使它类似于ASP.NET中的母版页,因为它需要一个控制器。有没有办法创建一个有自己控制器的布局
2) 如何在不进行回发的情况下在局部视图中重定向,即将包含先前由Ajax调用的局部视图的div更新为编辑视图
3) 我希望这一切都有意义。我会看到一个个人基本信息的配置文件,我可以在该视图中按edit,基本信息控制器的edit视图将加载到div中,而无需回发。要做到这一点,最好的办法是什么?我可能误解了一些事情。 我认为您正在尝试将页面某部分的显示视图翻转为该部分页面的编辑视图 我将保持一般性,因为没有足够的代码可以直接引用 您应该针对可能发生的各种单击注册javascript事件处理程序(在单独文件中的jquery闭包中是我个人的首选)。这些处理程序应该请求所需的任何操作(返回部分视图)
e、 g.当有人单击编辑链接时,处理程序调用/GetEditStuff操作,获取部分视图,成功后,清除父div以前的内容,并将其替换为部分视图。我可能有误解。 我认为您正在尝试将页面某部分的显示视图翻转为该部分页面的编辑视图 我将保持一般性,因为没有足够的代码可以直接引用 您应该针对可能发生的各种单击注册javascript事件处理程序(在单独文件中的jquery闭包中是我个人的首选)。这些处理程序应该请求所需的任何操作(返回部分视图)
e、 g.当有人单击编辑链接时,处理程序调用/GetEditStuff操作,获取部分视图,成功后,清除父div之前的内容并将其替换为部分视图。您会发现使用jQuery ajax方法比使用
ajax.ActionLink()更容易
和Ajax.BeginForm()
方法。大体上
<button type="button" class="details" data-id="Model.UserName">View details</button>
<div id="content"></div>
var detailsUrl = '@Url.Action("Details", "User")';
var editUrl = '@Url.Action("Edit", "User")';
// Display the details view
$('.details').click(function() {
$.get(detailsUrl, { id: $(this.data('id') }, function(response) {
$('#content').html(response);
});
});
// Display the edit view
$('#content').on('click', '#edit', function() {
$.get(editUrl, { id: $(this.data('id') }, function(response) {
$('#content').html(response);
});
});
// Post the edit form and replace with the updated details view
$('#content').on('click', '#save', function() {
var id = $(this).data('id');
var data = $(this).closest('form').serialize();
$.post(editUrl, data, function(response) {
if (response) {
$.get(detailsUrl, { id: id }, function() {
$('#content').html(response);
});
} else {
// Oops
}
}).fail(function (result) {
// Oops
});
});
局部视图在哪里
_Details.cshtml
@model UserModel
.... // display properties of the model
<button type="button" id="edit" data-id="Model.UserName">Edit</button>
@model用户模型
.... // 显示模型的属性
编辑
_Edit.cshtml
@model UserModel
<form>
.... // controls for properties of the model
<button type="button" id="save" data-id="Model.UserName">Save</button>
</form>
@model用户模型
.... // 模型属性的控件
拯救
您会发现使用jQuery ajax方法比使用ajax.ActionLink()
和ajax.BeginForm()
方法更容易实现这一点。大体上
<button type="button" class="details" data-id="Model.UserName">View details</button>
<div id="content"></div>
var detailsUrl = '@Url.Action("Details", "User")';
var editUrl = '@Url.Action("Edit", "User")';
// Display the details view
$('.details').click(function() {
$.get(detailsUrl, { id: $(this.data('id') }, function(response) {
$('#content').html(response);
});
});
// Display the edit view
$('#content').on('click', '#edit', function() {
$.get(editUrl, { id: $(this.data('id') }, function(response) {
$('#content').html(response);
});
});
// Post the edit form and replace with the updated details view
$('#content').on('click', '#save', function() {
var id = $(this).data('id');
var data = $(this).closest('form').serialize();
$.post(editUrl, data, function(response) {
if (response) {
$.get(detailsUrl, { id: id }, function() {
$('#content').html(response);
});
} else {
// Oops
}
}).fail(function (result) {
// Oops
});
});
局部视图在哪里
_Details.cshtml
@model UserModel
.... // display properties of the model
<button type="button" id="edit" data-id="Model.UserName">Edit</button>
@model用户模型
.... // 显示模型的属性
编辑
_Edit.cshtml
@model UserModel
<form>
.... // controls for properties of the model
<button type="button" id="save" data-id="Model.UserName">Save</button>
</form>
@model用户模型
.... // 模型属性的控件
拯救
这看起来很棒!但是,如果视图外部存在对某些事件的处理,这并没有破坏详细信息和编辑视图的某种自包含封装规则吗?我还想加载多个视图,每个视图都有自己的逻辑和自己的编辑和保存URL。不,一点也不。但请注意,我只是在答案中给出了基本结构。还有很多其他的事情要考虑,例如你有客户端验证,在这种情况下,你需要在保存之前触发它。这看起来很棒!但是,如果视图外部存在对某些事件的处理,这并没有破坏详细信息和编辑视图的某种自包含封装规则吗?我还想加载多个视图,每个视图都有自己的逻辑和自己的编辑和保存URL。不,一点也不。但请注意,我只是在答案中给出了基本结构。还有很多其他的事情要考虑,例如,你有客户端验证,在这种情况下,你需要在保存之前触发它。