Mal*_*olm 11 ajax asp.net-mvc asp.net-mvc-partialview
我在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>
Run Code Online (Sandbox Code Playgroud)
当这运行时,IngredientsListControl.ascx在浏览器中显示一个新页面,并且不会更新ingredientlistdiv.
这是我的控制器动作
[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");
}
}
Run Code Online (Sandbox Code Playgroud)
我在这条线上做得对吗?
return PartialView("IngredientsListControl", recipe.Ingredients);
Run Code Online (Sandbox Code Playgroud)
这是我如何将控件渲染到div中,因此它不会加载新页面.???
马尔科姆
当你使用这个:
<a href="javascript:document.forms[0].submit()">
Run Code Online (Sandbox Code Playgroud)
......你应该知道它与它不一样
<input type="submit" />
Run Code Online (Sandbox Code Playgroud)
它不会引发onsubmit事件,也不会调用MVC的AJAX事件处理程序.
要确认这是问题,请添加
<input type="submit" />
Run Code Online (Sandbox Code Playgroud)
在表单内部并尝试一下.
最后,只需从链接中调用onsubmit()即可
<a href="#" onclick="document.forms[0].onsubmit()">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29662 次 |
| 最近记录: |