【问题标题】:How to convert PartialViewResult to MvcHtmlString in MVC3?如何在 MVC3 中将 PartialViewResult 转换为 MvcHtmlString?
【发布时间】:2012-07-28 09:49:02
【问题描述】:

如何在MVC3中将PartialViewResult转换为MvcHtmlString?

我想更改 HTML 字符串并从某些 Partial 视图中替换导致 PartialViewResult 中的某些 ID 索引。

 e.g.
 <input type="hidden" value="1" name="[0].lstMatches[4].match_rule_id"> 
 to
 <input type="hidden" value="1" name="[0].lstMatches[6].match_rule_id">

模型喜欢

return PartialView("_MatchPartial", objRETURN).TOMvcHTMLSTRING();

正在寻找这种 TOMvcHTMLSTRING() 类型的函数。

有人可以帮我吗?

提前致谢, 阿米特·帕贾帕蒂

【问题讨论】:

  • 为什么需要这个?目前尚不清楚原始问题是什么。你到底想改变什么,为什么?请提供更多详细信息。
  • 根据您的建议,我在 Partialview 上使用了 IList。如何通过@Ajax.actionlink,我动态地将部分视图添加到 div 并且这个 Div 包含带有 的控件,当我在这个 Div 上做 Ajax 发布时,我没有得到这个动态添加的额外记录,而是得到已经存在的记录,例如name= [0].lstMatches[6].match_rule_id,我的模型不包含新记录,所以,我想我需要生成具有正确索引的 HTML。可以?还是其他方式?
  • 请发布您迄今为止尝试过的完整代码?
  • Main view 和 PartialView 很长,在这里发帖不可行。

标签: asp.net-mvc-3 razor


【解决方案1】:
protected string RenderView(PartialViewResult result)
{
  using (var sw = new StringWriter())
  {
    result.View = ViewEngines.Engines.FindPartialView(ControllerContext, result.ViewName).View;
    ViewContext vc = new ViewContext(ControllerContext, result.View, result.ViewData, result.TempData, sw);
    result.View.Render(vc, sw);
    return sw.GetStringBuilder().ToString();
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多