将 viewmodel 传递给 ajax 控制器操作,返回该模型的部分内容?
我有一个强类型的视图。在这个视图中,我有 jqueryui 选项卡,单击时调用我的控制器并返回部分视图
("#tab0").load('@Url.Action("ProfileImage", "User")');
public ActionResult ProfileImage()
{
return PartialView("_ProfileImage");
}
我想做的是将模型从“父”视图传递到控制器,然后控制器可以将其绑定到部分视图返回:
("#tab0").load('@Url.Action("ProfileImage", "User", new {model=model})');
public ActionResult ProfileImage(UserViewModel model)
{
return PartialView("_ProfileImage", model);
}
这可能吗?这通常是如何完成的?您在一个视图中拥有模型数据,并且希望将其传递到异步加载的部分视图吗?
I have a view that is strongly typed. Inside this view i have jqueryui tabs, that when clicked call my Controller and return a partial view
("#tab0").load('@Url.Action("ProfileImage", "User")');
public ActionResult ProfileImage()
{
return PartialView("_ProfileImage");
}
What I'd like to do is pass the model from the "parent" view to the controller which can then bind it to the partial when it is returned:
("#tab0").load('@Url.Action("ProfileImage", "User", new {model=model})');
public ActionResult ProfileImage(UserViewModel model)
{
return PartialView("_ProfileImage", model);
}
Is this possible? how is this normally done? Where you have the model data in one view and you'd like to pass it to a asynchronously loaded partial view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在视图模型上创建一个 ToJson 方法,它可能是这样的:
它只是将视图模型序列化为 json。 IHtmlString 返回类型确保输出不会在您的视图中进行编码。
对控制器的调用将如下所示:
json modelbinder 可以在服务器端重新创建视图模型。一路上你可能会遇到一些问题,但我想没有什么是无法解决的。
You could create a ToJson method on your viewmodel, which could be something like this:
It just serializes the viewmodel into a json. The IHtmlString returntype makes sure the output isn't encoded in your view.
The call to your controller would be something like this:
The json modelbinder can recreate the viewmodel on the serverside. You probably run into some problems along the way, but nothing unsolvable I guess.