asp.net-mvc - ASP.NET MVC 从 POST 方法渲染部分内容

标签 asp.net-mvc partial-views

问题

我有 Telerik TabControl,每个选项卡内容都是部分 View 。当请求为 GET 时,一切顺利:

//
// GET: /ProductVersion/Translations        
public ActionResult Translations(Guid id)
{
    VersionEditTabViewModel model = CreateTranslationsViewModel(id);
    return PartialView("Translations", model);
}

现在的问题是,在某些选项卡上,我有一个表单,其中包含触发提交事件的控件。

[HttpPost]
public ActionResult Translations(Guid id)
{
    FormCollection formCollection = new FormCollection(Request.Form);
    string message = string.Empty;
    int languageId = int.Parse(formCollection["TranslationsLanguageList"]);
    string action = formCollection["TranslationAction"];
    if(action == Constants.translation_save)
    {
        _translationModel.SaveTranslations(formCollection);
        message = "Translation information saved";
    }
    else if (action == Constants.translation_language_changed)
    {
/*
    PROBLEM: causes whole page to render, not partial
*/
        return PartialView("Translations", model);
    }
    return RedirectToAction( ... updates the complete page not only partial ...);
}

我的问题是:如何从 POST 方法渲染部分内容?因为现在使用该源代码选项卡内容将加载到整个页面,而不是选项卡内。

解决方案

我必须将 DIV 放在 Ajax.Form 之外,而且我的 DropDownList 上的提交也不正确。我所做的是用 Id 创建隐藏的提交按钮,然后使用 jQuery 执行它的点击事件。

最佳答案

如需更多引用,请参阅 SO 上的此问题:

MVC - Using Ajax to render partial view

这显示了 Ajax.BeginForm 的完整实现以及周围的 DIV 和内部表单控件。您应该能够将整个设置(DIV + 表单 + HTML 表单元素)放置在 Telerik 选项卡中,如下所示:

<% Html.Telerik().TabStrip()
        .Name("TabStrip")
        .Items(tabstrip =>
        {
            tabstrip.Add()
                    .Text("Your Tab Text")
                    .Content(() =>
                    {%>
                        <div id="containerDiv" align="left">
                           <% using (Ajax.BeginForm("Example", "Controller/Action", new AjaxOptions { UpdateTargetId = "containerDiv" })){ %>
                           <%-- Render Partial here -->
                           <% } %>
                        </div>
                    <%});

希望有帮助。

关于asp.net-mvc - ASP.NET MVC 从 POST 方法渲染部分内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289176/

相关文章:

asp.net-mvc-3 - 如何使用Html.Partial()方法呈现具有显式路径的局部 View

c# - 对象引用未设置为局部 View 中对象的实例

c# - user.isuserinrole mvc 的自定义角色提供程序

javascript - 在调用通过同一函数 [ session 超时] 显示计时器的 JavaScript 函数之前清除标签

javascript - 进度条问题

asp.net-mvc - bootstrap 4.0 导航栏链接未使用 Razor 重定向

asp.net - 如何在网络表单中包含部分 View

asp.net-mvc - 在 MVC 中将 PDF(存储为 BLOB)显示为图像

asp.net-mvc - 如何将默认打开页面设置为区域的操作

ruby-on-rails-3 - Rails 部分有时需要太多时间来渲染