ASP.NET MVC 和强类型局部视图
我正在使用 AJAX 调用加载部分视图:
public ActionResult LoadServerForm()
{
//data stuff
ViewData["ApplicationID"] = appID.ToString();
ViewData["Servers"] = ServersList(appServerRep.Session, null, appServers);
return PartialView("Application_AddServer");
}
这效果很好,但我试图摆脱神奇的 ViewData 字符串。我尝试使部分视图从与“托管”页面相同的 ViewModel 继承,但是当我在部分视图中尝试执行此操作时,模型对象为 null:
<%= Html.HiddenFor(model=>model.Application_Key, Model.Application_Key) %>
有没有办法将主页 ViewModel 向下传递到 AJAX 加载的PartialView 还是我应该寻找一种不同的方法?
I'm loading a partial view with an AJAX call:
public ActionResult LoadServerForm()
{
//data stuff
ViewData["ApplicationID"] = appID.ToString();
ViewData["Servers"] = ServersList(appServerRep.Session, null, appServers);
return PartialView("Application_AddServer");
}
This works great, but I'm trying to get away from magic ViewData strings. I tried making the partial view inherit from the same ViewModel as the "hosting" page, but the Model object is null when I try to this in the partial view:
<%= Html.HiddenFor(model=>model.Application_Key, Model.Application_Key) %>
Is there a way to pass the main page ViewModel down into the AJAX-loaded PartialView or should I be looking for a different approach altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您
返回 PartialView("Application_AddServer");
时,您必须传递模型:由于这是一个 AJAX 请求,因此它是一个单独的控制器操作调用,并且新的 PartialView 不知道该模型请求页面的。您必须根据原始数据源或通过 AJAX 请求传递的数据来重建它。
When you
return PartialView("Application_AddServer");
, you have to pass the model:Since this is an AJAX request, it's a separate controller action invocation, and the new PartialView doesn't know about the model of the requesting page. You'll have to reconstruct it, either from whatever your original data source is or from data passed with the AJAX request.