質問

私は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は、新しいページをdisplayas ブラウザ内と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イベントが発生しない、とMVCのAJAXのイベントハンドラが呼び出されません。

これが問題であることを確認するには、追加

<input type="submit" /> 

フォーム内とそれを試してみます。

最後に、ちょうどあなたのリンクからのonSubmit()を呼び出す

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

他のヒント

あなたが正しくあなたのページ(マスターページ)にajaxmvcとjQueryのスクリプトを参照していることを保証する価値があるかもしれません。これらが正しくない場合は、新しいページではなく、ターゲットのdiv内の正しい出力で表示されます。

れるrenderPartialはアクション名ではなく、ユーザーコントロールの名前を取り、その「UpdateStep2」、あなたのアクション名を持つ「IngredientsListControl」を置き換えます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top