简体   繁体   English

Asp.Net MVC-从控制器操作重现纯ViewModel

[英]Asp.Net MVC - returing pure ViewModel from controller action

I use Asp.Net MVC 5.0 and I'd like to "talk" with controller in my layout page. 我使用Asp.Net MVC 5.0,并且想在布局页面中与控制器“交谈”。 I want to call ProfileController.GetProfileInformationViewModel method in my Layout page to get ProfileViewModel class object. 我想打电话给ProfileController.GetProfileInformationViewModel在我的布局页方法来获得ProfileViewModel类对象。 Is it possible without returning ActionResult ( View/PartialView ) and using something like HtmlAction in Razor ? 是否可以不返回ActionResultView/PartialView )并在Razor中使用类似HtmlAction的方法? I do not want to return any html, I want to keep it decoupled and reusable. 我不想返回任何html,我想保持它解耦和可重用。

Do you mean you want to return something like JSON data and get this via JavaScript? 您是说要返回JSON数据之类的内容并通过JavaScript来获取它吗?

public JsonResult GetProfileInformationViewModel(int id)
{
    var myProfile = GetProfile(id);
    return new JsonResult(){ Data = myProfile };
}

Then you can use an ajax call to get the object. 然后,您可以使用ajax调用来获取对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM