2011-09-22 78 views
2

我正在一个项目在asp.net mvc3(c#)。Asp.net Mvc转换返回行动结果(视图)字符串

我需要一个将视图(而不是局部视图)转换为来自不同控制器的字符串的解决方案。

代码Explantion:

1)调用 “细节” 从proposalsController proposalmoduleController的作用。

2)proposalmoduleController action“details”返回一个视图,并将这个视图(返回结果)作为一个字符串转换为proposalsController。

代码

public class proposalmoduleController : ControllerBase 
{ 

    [HttpGet] 
    public ActionResult details(int id, int widgetuniqueid) 
    { 
     //id - widgetid of div container 
     List<ModuleViewModel> listmoduleviewmodel = new List<ModuleViewModel>(); 
     List<ModuleFieldViewModel> listmodulefieldviewmodel = new List<ModuleFieldViewModel>(); 

     var objProposalModuleService = new ProposalModuleService(); 
     var objModuleViewModel = new ModuleViewModel(); 

     string WidgetTitle = ""; 
     Int64 ModuleTemplateID = 0; 


     //objModuleViewModel.ProposalID = proposalid; 
     objModuleViewModel.ProposalModuleWidgetID = id; 

     listmoduleviewmodel=objProposalModuleService.Select(1, objModuleViewModel,out listmodulefieldviewmodel, out WidgetTitle, out ModuleTemplateID); 

     return View(listmoduleviewmodel); 
    } 
} 

    public class proposalsController : ControllerBase 
    { 
     public string SaveHtml(int ProposalID) 
     { 
      var objProposalSortOrderViewModelList = new List<ProposalSortOrderViewModel>(); 
      proposalmoduleController objModuleController = new proposalmoduleController(); // Initilize the object of proposalmoduleController for accessing details method 

      objProposalSortOrderViewModelList = GetProposalSortorders(ProposalID); 

      string result; 
      foreach (var item in objProposalSortOrderViewModelList) 
      { 
       ViewResult viewResult = (ViewResult)objModuleController.details(Convert.ToInt32(item.KeyID), Convert.ToInt32(item.SortOrder)); // Fetch the result returned from proposalmodulecontroller,details action 
       result=viewResult.ToString();   // Need to get result fetch from the proposalmodulecontroller,details action as a string 
      } 
     } 
    } 
enter code here 

请提出解决方案。

+0

您正在使用哪个视图引擎?这在剃刀中非常简单。 –

+0

现在使用ASPX视图引擎 – amexn

回答

0

A ViewResult不是视图。由MVC引擎使用ViewResult来确定必须呈现的视图。 我认为,如果你改变你的观点,这是更好:如果你想在一个视图中的局部视图只是使用@Html.Partial

  • ,如果你想在你的proposalsController细节数据演示代码工作

    • 请不要拨打proposalmoduleController的操作,而是调用一种服务方法,为您提供数据
  • 相关问题