سؤال

لدي هذه العلامة في تطبيق 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>

عندما يقوم هذا يدير IngredientsListClistControl.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");
            }
        }

هل أفعل الشيء الصحيح في هذا الخط؟

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

هل هذه الطريقة التي أجعلها تجعل التحكم في Div بحيث لا يتم تحميل صفحة جديدة. ؟؟

مالكولم

هل كانت مفيدة؟

المحلول

عند استخدام هذا:

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

... يجب أن تكون على علم بأنها ليست هي نفسها

<input type="submit" />

لا يرفع حدث Onsubmit، ولا يسمى AJAX EventHandler MVC.

لتأكيد هذه هي المشكلة، إضافة

<input type="submit" /> 

داخل النموذج وجربه.

أخيرا، اتصل فقط Onsubmit () من رابطك

<a href="#" onclick="document.forms[0].onsubmit()">

نصائح أخرى

قد يكون من المستحق التأكد من الإشارة بشكل صحيح إلى البرامج النصية AJAXMVC و JQUERY في صفحتك (صفحة رئيسية). إذا كانت هذه غير صحيحة، فسيتم عرض صفحة جديدة بدلا من الإخراج الصحيح في DIV الهدف.

يستغرق Renderpartial اسم عمل، وليس اسم عنصر تحكم المستخدم، لذلك استبدل "IncredientListClistControl" باستخدام "UpdateStep2"، اسم عملك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top