Asp.net转换的mvc行动回报结果(视图)字符串 [英] Asp.net Mvc Convert return Action result(view) to string
问题描述
我工作在asp.net MVC3(C#)的一个项目。
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:
Code Explantion:
1),叫细节,从proposalsController proposalmoduleController的行动。
1) Calling "details" action of proposalmoduleController from proposalsController.
2)proposalmoduleController行动细节返回一个视图,并转换这一观点(返回结果),如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 = 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
请提出任何解决办法。
推荐答案
A 的ViewResult
不是一个视图。 的ViewResult
所使用的MVC引擎确定必须呈现的视图。
我认为这是更好,如果你改变你的观点:
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:
- 如果您想在一个视图的局部视图上使用presentation code只是工作
@ Html.Partial
- 如果你想获得在细节数据的
proposalsController
不叫的行动proposalmoduleController
,但打电话,让你的数据的服务方法
- 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 theproposalmoduleController
but call a service method that gives you the data
这篇关于Asp.net转换的mvc行动回报结果(视图)字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!