Tôi có đánh dấu này trong ứng dụng MVC.MVC - Sử dụng Ajax để hiển thị một phần xem
<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>
Khi điều này chạy IngredientsListControl.ascx displayas một trang mới trong trình duyệt và không cập nhật ingredientlistdiv.
Đây là điều khiển hành động của tôi
[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");
}
}
Am Tôi làm đúng trên dòng này?
return PartialView("IngredientsListControl", recipe.Ingredients);
Đó có phải là cách tôi đưa điều khiển vào div để không không tải trang mới. ???
Malcolm
Chúng tôi luôn sử dụng, đường dẫn ứng dụng tương đối đầy đủ cho tên một phần, ví dụ Html.RenderPartial ("~/Views/Home/ModuleNewUser.ascx") –
Nó có hiển thị một phần như một trang mới không? Hoặc một số trang khác? Nội dung của bạn là gì? –