【问题标题】:MVC - Using Ajax to render partial viewMVC - 使用 Ajax 渲染局部视图
【发布时间】:2010-10-19 12:42:57
【问题描述】:

我在 MVC 应用程序中有这个标记。

<div id="ingredientlistdiv">
    <% Recipe recipe = (Recipe)Model; %>
    <% Html.RenderPartial("IngredientsListControl", recipe.Ingredients); %>
</div>

<div id="ingrediententrydiv" align="left">
    <% using (Ajax.BeginForm("Add Ingredient", "Recipes/UpdateStep2", new AjaxOptions { UpdateTargetId = "ingredientlistdiv" }))
       { %>
    <p>
        <label for="Units">Units:</label><br />
        <%= Html.TextBox("Units", 0)%>
        <%= Html.ValidationMessage("Units", "*")%>
    </p>
    <p>
        <label for="Measure">Measure:</label><br />
        <%= Html.TextBox("Measure")%>
        <%= Html.ValidationMessage("Measure", "*")%>
    </p>
    <p>
        <label for="IngredientName">Ingredient Name:</label><br />
        <%= Html.TextBox("IngredientName")%>
        <%= Html.ValidationMessage("IngredientName", "*")%>
    </p>
    <p><a href="javascript:document.forms[0].submit()">Save Ingredient</a></p>
    <%= Html.Hidden("RecipeID", recipe.RecipeID.ToString())%>
    <% } %>
</div>

当它运行时,IngredientsListControl.ascx 显示为一个新页面 在浏览器中并且不更新成分列表div。

这是我的控制器操作

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateStep2(FormCollection form)
        {
            var factory = SessionFactoryCreator.Create();

            using (var session = factory.OpenSession())
            {
                Recipe recipe = GetRecipe(session, Convert.ToInt32(form["RecipeID"]));

                Ingredient ingredient = new Ingredient();

                ingredient.UpdateFromForm(form);
                ingredient.Validate(ViewData);

                if (ViewData.ModelState.Count == 0)
                {
                    recipe.Ingredients.Add(ingredient);
                    session.Save(recipe);
                    return PartialView("IngredientsListControl", recipe.Ingredients);
                }


                return Content("Error");
            }
        }

我在这条线上做对了吗?

return PartialView("IngredientsListControl", recipe.Ingredients);

这就是我将控件渲染到 div 中的方式吗? 不加载新页面。???

马尔科姆

【问题讨论】:

  • 我们一直使用完整的、应用相对路径作为部分名称,例如Html.RenderPartial("~/Views/Home/ModuleNewUser.ascx")
  • 是否将部分显示为新页面?还是其他页面?你的部分内容是什么?

标签: ajax asp.net-mvc asp.net-mvc-partialview


【解决方案1】:

当你使用这个时:

<a href="javascript:document.forms[0].submit()">

...你应该知道它和

不一样
<input type="submit" />

它不会引发 onsubmit 事件,也不会调用 MVC 的 AJAX 事件处理程序。

要确认这是问题,请添加

<input type="submit" /> 

在表单中尝试一下。

最后,只需从您的链接中调用 onsubmit()

<a href="#" onclick="document.forms[0].onsubmit()">
【解决方案2】:

可能值得确保您在页面(母版页)中正确引用了 ajaxmvc 和 jquery 脚本。如果这些不正确,将显示一个新页面,而不是目标 div 中的正确输出。

【讨论】:

  • 正是我想说的。光看就找不到其他原因了。 :)
【解决方案3】:

RenderPartial 采用动作名称,而不是用户控件的名称,因此将“IngredientsListControl”替换为“UpdateStep2”,即您的动作名称。

【讨论】:

  • 对不起,但我很确定它不会。这就是 RenderAction 所做的,它包含在 MvcFutures 中。
猜你喜欢
  • 2011-04-08
  • 2011-11-17
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多