【问题标题】:How to render partial view in some specific div without using ajax in asp.net mvc 3?如何在某些特定 div 中呈现部分视图而不在 asp.net mvc 3 中使用 ajax?
【发布时间】:2016-02-17 23:27:35
【问题描述】:

如何在不使用 ajax 在 asp.net mvc 3 中渲染某些特定 div 中的部分视图???

例如我有一个模型:

public class TestViewModel
{
    public string FullText
    {
        get
        {
            retrun "Full text";
        }
    }
    public string ShortText
    {
        get
        {
            return "Short text";
        }
    }
}

主视图:

@model TestViewModel
   ...      
    <div id="RenderHere">

    </div>
    @Html.ActionLink("Show short text");
    @Html.ActionLink("Show full text");
   ...

还有两个部分视图:

ShortTextParial

@model TestViewModel
@Html.LabelFor(m => m.ShortText);
@Model.ShortText;

和全文部分

@model TestViewModel
@Html.LabelFor(m => m.FullText);
@Model.FullText;

如何在不使用 ajax 的情况下按下“显示短文本”操作链接后在“RenderHere”div 中呈现 ShortTextPartial?我已经找到了这样的解决方案,但它们不适合我。

附:我不想使用 Ajax,因为如果在客户端禁用 Java 脚本,页面将无法正常工作。

【问题讨论】:

  • 您可以将部分内容缓存在客户端内存中,然后在点击时显示。 razor 引擎仅在编译时可用,在运行时不可用。因此,ajax 是在运行时与服务器交互而无需刷新页面的唯一方式。
  • 你在与粮食作斗争,你为什么不想使用 Ajax?您可以使用@Ajax.BeginForm 代替@Html.BeginForm 并将div 设置为更新为参数。轻松搞定。
  • 我不想使用 Ajax,因为如果在客户端禁用 Java 脚本,页面将无法正常工作。
  • 您可以添加查询字符串参数来控制要显示的文本。这意味着您将重新加载整个页面只是为了那一点点文字......

标签: c# asp.net-mvc asp.net-mvc-3


【解决方案1】:

首先我不得不说我不认为你的设计是最好的。请注意,大多数逻辑、比较和条件应该发生在您的模型和业务逻辑层中,而不是在视图和控制器中。

无论如何,如果您不想使用 AJAX,您应该使用 URL 参数或会话状态在页面之间传递数据。说了这么多,我是通过URL参数开发的。

(我的开发环境是VS2010 SP1,ASP.NET 4,MVC 3)

首先我们需要像这样改变TextViewModel:

public class TextViewModel
{
    public string FullText { get { return "Full text"; } }
    public string ShortText { get { return "Short text"; } }

    public string ViewMode { get; private set; }

    public TextViewModel(string viewMode)
    {
        if (!string.IsNullOrWhiteSpace(viewMode))
            this.ViewMode = viewMode.Trim().ToLower();
    }
}

现在对控制器进行一点调整:

public class HomeController : Controller
{
    public ActionResult Index(string id)
    {
        Models.TextViewModel model = new Models.TextViewModel(id);
        return View(model);
    }
}

最后是我们的观点:

@model MvcApplication1.Models.TextViewModel
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<div id="RenderHere">
    @if (Model.ViewMode == "short")
    {
        @Html.Partial("ShortTextParial", Model)
    }
    @if (Model.ViewMode == "full")
    {
        @Html.Partial("FullTextPartial", Model)
    }
</div>
@Html.ActionLink("Show short text", "Index", new { id = "short" })
@Html.ActionLink("Show full text", "Index", new { id = "full" })

该应用程序在我的计算机上运行良好。如果您需要,我已在此处上传代码:http://goo.gl/9v2Ny

我再说一遍,你可以想出一个更好的设计,它不需要在 View 甚至在 Controller 中使用任何 @if

【讨论】:

    【解决方案2】:

    如果您真的不想使用 ajax 来显示 more 数据,您可以在单独的 div 中加载这两个内容并隐藏完整视图,当用户单击“显示更多”链接时, 显示出来。

    你真的想这样做吗?请记住,并非所有用户都会点击您的“显示更多”链接。您仍然会为用户加载内容并将其隐藏。为什么需要这样做并增加页面大小?如果可能的话,我会使用 ajax 解决方案来“按需”提供数据。

    【讨论】:

      猜你喜欢
      • 2011-07-23
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2015-05-22
      相关资源
      最近更新 更多