如何使用 MVC3 在视图中的某个位置插入部分视图?
我有一个 MVC3 应用程序,正在将 pjax 实现为 .一切都运行良好,除了当加载的地址尚未在客户端具有主视图时在服务器端执行的操作之外。我的控制器代码看起来像
public virtual ActionResult Details(Guid id)
{
var partDetail = new PartDetail(id);
var partialView = PartialView("Details", partDetail);
if(Request.Headers["X-PJAX"]!= null)
{
return partialView;
}
var mainView = View("Index");
// Stick Partial View into main view at #update_panel?
return mainView;
}
如何将部分视图粘贴到主视图中,以便它将部分视图插入#update_panel?
I have an MVC3 application that I am implementing pjax into . Everything is working well except what to do on the server side when an address gets loaded that doesn't already have the main view on the client side. My Controller code looks like
public virtual ActionResult Details(Guid id)
{
var partDetail = new PartDetail(id);
var partialView = PartialView("Details", partDetail);
if(Request.Headers["X-PJAX"]!= null)
{
return partialView;
}
var mainView = View("Index");
// Stick Partial View into main view at #update_panel?
return mainView;
}
How can I stick the partial View into the main view so it inserts the partial view in the #update_panel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,无需进行重大重构,您可以执行以下操作。
(这假设您能够将 index.cshtml 上的
@model
设置为PartDetail()
)。在上面的控制器操作中,将:更改
为:
然后,在index.cshtml中添加以下内容:
正如我所说,只有当索引@model设置为
PartDetail()
时,这才有效,否则,需要对索引视图中的模型进行一些重构才能包含此PartDetail()
模型。这个视图模型很可能如下所示:这个重构的视图模型将作为
@model IndexViewModel
添加到 index.cshtml 中,并由部分使用:希望这是有道理的。
Ok, without a major refactor, you could do the following.
(this assumes that you are able to set the
@model
on index.cshtml toPartDetail()
).in your controller action above, change:
to:
then, inside your index.cshtml, add the following:
As i said, this will ONLY work if the index @model is set to
PartDetail()
, otherwise, a little refactoring on the model in the index view will be required to include thisPartDetail()
model. this viewmodel might well look like the following:this refactored viewmodel would be added to the index.cshtml as
@model IndexViewModel
and consumed by the partial as:hope this makes sense.