0

ドロップダウン メニューから選択したときに、「現在表示されているページ」の末尾に部分ビューを追加しようとしています。

これは私の見解からのドロップダウンです:

<div>
        @Html.DropDownListFor(x => x.DropDownInfo, Model.Info, "DefaultSelection")
        @Html.ValidationMessageFor(model => model.Courses)
</div>

私はここJqueryで最も必要としています。コントローラーから返された PartialView を追加するにはどうすればよいですか (以下に貼り付けます)。私の現在のJquery:

$(document).ready(function() {
    $("#DropDownInfo").change(function() {
        var strSelected = "";
        $("#DropDownInfo option:selected").each(function() {
            strSelected += $(this)[0].value;
        });
        var url = "/Controller/PreFillMethod/?MethodString=" + strSelected;

        $.post(url, function(data) {
            //*****
            // Assuming everything else is correct,
            // what do I do here to have my partial view returned 
            // at the end of the currently displayed page?
            //*****
        });
    });
});

これは、PartialView で応答するコントローラーの一部です (ドロップダウン選択からの文字列をこのコントローラーに渡して、最終的に PartialView のフォームのフィールドに入力するために使用する必要があります)。

public PartialViewResult PreFillCourse(string selectedFromDropDown)
{
    ViewBag.selectedString = selectedFromDropDown;
    MyViewModel preFill = new MyViewModel
    {
        Title = selectedFromDropDown, // I am using this to pre-fill a field in a form
    };
    return PartialView("_PartialViewForm", preFill);
}

部分的なビュー (重要な場合):

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>CourseTemplates</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

私が状況に完全に間違ってアプローチしている場合、私は提案を受け入れます。

私の目標は、ユーザーがドロップダウン メニューから「テンプレート」を選択し、そのテンプレートのデータがドロップダウンの下のフォームに自動入力されるようにすることです。

私の Jquery は非常にラフです -この投稿をガイドとして使用しています

4

1 に答える 1

1

ビューにdivが必要です

<div id ="divToAppend">
</div>

次に、部分ビューをdivに追加します

$(document).ready(function() {
    $("#DropDownInfo").change(function() {
        var strSelected = "";
        $("#DropDownInfo option:selected").each(function() {
            strSelected += $(this)[0].value;
        });
        var url = "/Controller/PreFillMethod/?MethodString=" + strSelected;

        $.post(url, function(data) {
             $('#divToAppend').html(data);

            //*****
            // Assuming everything else is correct,
            // what do I do here to have my partial view returned 
            // at the end of the currently displayed page?
            //*****


          });
    });
});
于 2012-04-17T17:09:07.293 に答える