开发者

Asp.net Mvc Convert return Action result(view) to string

I am working on a project in asp.net mvc3(c#).

I need a solution for convert a view(not a partial view) to string from different controllers.

Code Explantion:

1) Calling "details" action of proposalmoduleController from proposalsController.

2) proposalmoduleController action "details" returns a view and convert this view(return result) as a string in proposalsController.

Code

public class proposalmoduleController : ControllerBase
{   

   [HttpGet]
    public ActionResult details(int id, int widgetuniqueid)
    {
        //id - widgetid of div container
        List<ModuleViewModel> listmoduleviewmodel = ne开发者_StackOverflow社区w 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

Please suggest any solution.


A ViewResult is not a View. ViewResult is used by the MVC engine to determine the view that must be rendered. I think it's better if you change your perspective:

  • if you want to include a partial view in a view just work on the presentation code using @Html.Partial
  • if you want to get the details data in your proposalsController don't call the action of the proposalmoduleController but call a service method that gives you the data
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜