0

ドロップダウン メニューで選択したオプションが hiddenfor 値に割り当てられる JavaScript を書いています。この hiddenfor はモデル プロパティ (SelectedModule) を使用しています。

送信ボタンをクリックすると、javascript で値を割り当てたにもかかわらず、model.SelectedModule に null 値が含まれます。

意見

@model UserManager.Models.vw_UserManager_Model
@{
    ViewBag.Title = "EditUser";
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <div id="edit-user">
        <fieldset>
            <legend>Edit user details</legend>
            <div class="editor-field">
                @Html.DropDownListFor(Model => Model.salutation, new List<SelectListItem>
                     {
                        new SelectListItem{ Text="Mr", Value = "Mr" }, 
                        new SelectListItem{ Text="Mrs", Value = "Mrs" },
                        new SelectListItem{ Text="Miss", Value = "Miss" },
                        new SelectListItem{ Text="Ms", Value = "Ms" },
                        new SelectListItem{ Text="Dr", Value = "Dr" }
                     })
                @Html.ValidationMessageFor(model => Model.salutation)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.firstname)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.firstname)
                @Html.ValidationMessageFor(model => model.firstname)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.lastname)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.lastname)
                @Html.ValidationMessageFor(model => model.lastname)
            </div>
            @{Html.Partial("~/Views/Partial/_AutocompleteGroupName.cshtml", this.ViewData);}
            <div class="editor-label">
                @Html.LabelFor(model => model.isactive)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model.isactive)
                @Html.ValidationMessageFor(model => Model.isactive)
            </div>
            <div class="editor-label">
                @Html.Label("Is approved")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model.IsApproved)
                @Html.ValidationMessageFor(model => Model.IsApproved)
            </div>
            <div class="editor-label">
                @Html.Label("Maximum concurrent users")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model.MaxConcurrentUsers)
                @Html.ValidationMessageFor(model => Model.MaxConcurrentUsers)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.email)
                @Html.ValidationMessageFor(model => model.email)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.rowtype)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.rowtype, new { @readonly = "readonly", @id = "txtNonEditableRowType" })
                - Non editable
            </div>
            <div class="editor-label">
                @Html.Label("Current Module")
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.module_name, new { @readonly = "readonly", @id = "txtNonEditableModule" })
                - Non editable
                @Html.ValidationMessageFor(model => model.module_name)
            </div>
            <br />
            @if (Model.rowtype == "ALF")
            {
                <div id="alfModules">
                    @Html.Label("New module")
                    <br />
                    @{Html.RenderAction("_CreateUserModulesAlf", "UserManager");}
                </div>
            }
            @if (Model.rowtype == "BRAD")
            {
                <div id="bradModules">
                    @Html.Label("New module")
                    <br />
                    @{Html.RenderAction("_CreateUserModulesBrad", "UserManager");}
                </div>
            }
            <div class="editor-label">
                @Html.LabelFor(model => model.group_name)
            </div>
            @* <div class="editor-field">
                @Html.EditorFor(model => model.group_name)
                @Html.ValidationMessageFor(model => model.group_name)
            </div>*@
            <div class="editor-label">
                @Html.Label("Current Group")
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.group_name, new { @readonly = "readonly", @id = "txtNonEditableGroup" })
                - Non editable
                @Html.ValidationMessageFor(model => model.group_name)
            </div>
            @Html.HiddenFor(model => Model.selected_module, new { id = "hdnSelectedModule" })
@*            @Html.HiddenFor(model => Model.selected_moduleAlf, new { id = "hdnSelectedModuleAlf" })
            @Html.HiddenFor(model => Model.selected_moduleBrad, new { id = "hdnSelectedModuleBrad" })*@
            <br />
            <fieldset style="width: 400px; padding-left: 15px;">
                <legend>Group Checker</legend>
                <div id="createuser-groupnamesearch">
                    @{Html.RenderAction("_txtGroupSearchForm", "UserManager");}
                </div>
            </fieldset>
            <p>
                <input type="submit" value="Edit" onclick="newModule()" />
            </p>
            <br />
            @Html.ActionLink("Back to User Manager Dashboard", "Index")
        </fieldset>
    </div>




}
<script type="text/javascript">

    $("#group_name").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("LookUpGroupName", "UserManager")',
                dataType: "json",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    value: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        //                            alert(item.group);
                        return {
                            label: item.group,
                            value: item.group
                        } // end of return

                    })); // end of response

                }, // end of success
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                } // end of error
            }); // end of ajax
        },
        minLength: 2,
        select: function (event, ui) { // Assign to hidden values to trigger onchange ajax call.

            $.ajax({
                url: '@Url.Action("GroupnameCheck", "UserManager")',
                dataType: "json",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    value: ui.item.label
                },
                success: function (data) {
                    $.each(data, function (index, value) {
                        if (index == "AlfGroup") {
                            $("#txtGroupnameExistsAlf").val(value);
                            if ($("#txtGroupnameExistsAlf").val() == "Alf Group doesn't exist.") {
                                $("#txtGroupnameExistsAlf").css("background-color", "red");
                            }
                            else {
                                $('#txtGroupnameExistsAlf').css("background-color", "#33ff00");
                            }
                        }

                        if (index == "BradGroup") {
                            $("#txtGroupnameExistsBrad").val(value);
                            if ($("#txtGroupnameExistsBrad").val() == "Brad Group doesn't exist.") {
                                $("#txtGroupnameExistsBrad").css("background-color", "red");
                            }
                            else {
                                $('#txtGroupnameExistsBrad').css("background-color", "#33ff00");
                            }
                        }
                    });
                }, // end of success
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                } // end of error
            }); // end of ajax

            $('#hdnGroupAlf').val(ui.item.label);
            $('#hdnGroupBrad').val(ui.item.label);
        },
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });

    function chkSelection() {
        var rowType = $("#txtNonEditableRowType").val();
        if (rowType == "ALF") {

            var selectedVal = $("#ddlSelectedAlf option:selected").val();
            $('#hdnSelectedModule').val(selectedVal);
        }
        else {

            var selectedVal = $("#ddlSelectedBrad option:selected").val();
            $('#hdnSelectedModule').text(selectedVal);
            alert(selectedVal);
        }
    }

    $(document).ready(function () {
        // Non editable fields grey
        $("#txtNonEditableGroup").css("background-color", "gray");
        $("#txtNonEditableModule").css("background-color", "gray");
        $("#txtNonEditableRowType").css("background-color", "gray");

        // Show/Hide group check part based on IF ALF or BRAD
        var rowType = $("#txtNonEditableRowType").val();

        if (rowType == "ALF") {
            $("#groupname-checker-alf").show();
            $("#groupname-checker-brad").hide();

            var selectedVal = $("txtNonEditableRowType").val();
            $('#hdnModuleAlf').val(selectedVal);

        }
        else {
            $("#groupname-checker-alf").hide();
            $("#groupname-checker-brad").show();

            var selectedVal = $("txtNonEditableRowType").val();
            $('#hdnModuleBrad').val(selectedVal);
        }

    });



    function newModule() { // Assign new selected module from dropdown to hidden form
        // so it can be used in model as selected_module
        if ($("#txtNonEditableRowType").val() == "ALF") {
            var val = $("#module_name :selected").val();
            $("#hdnSelectedModule").val(val);
        }
        else {
            var val = $("#module_name :selected").val();
            $("#hdnSelectedModule").val(val);
        }
    }  
</script>

コントローラ

[HttpPost]
public ActionResult EditUser(vw_UserManager_Model model)
{
    List<UserManager.Models.vw_UserManager_Model> modellist = new List<vw_UserManager_Model>();
    int outcome = 0;
    if (ModelState.IsValid)
    {
        outcome = UserManager.DAL.EditUser(model);
        modellist.Add(model);
    }

    if (outcome == 1)
    {

        if (modellist.FirstOrDefault().rowtype == "Alf")
        {

        }
        else
        {

        }

        return RedirectToAction("showSuccessUser", new
        {
            CrudType = "Edit",
            UserName = modellist.FirstOrDefault().UserName,
            Password = modellist.FirstOrDefault().password,
            FirstName = modellist.FirstOrDefault().firstname,
            LastName = modellist.FirstOrDefault().lastname,
            Email = modellist.FirstOrDefault().email,
            GroupName = modellist.FirstOrDefault().group_name,
            IsActive = modellist.FirstOrDefault().isactive,
            selected_module = modellist.FirstOrDefault().module_name
        });
    }

    else
    {
        ViewBag.Message = "Failure";
        return RedirectToAction("showError", model);
    }
}

概要

ドロップダウン メニューでアイテムが選択されると、その値が HiddenFor フィールドに割り当てられます。これは、値を見ることができるfirebugを使用して機能します。

問題は、フォームを送信するときに、C# コードの model.SelectedModule プロパティに null 値があることです。

誰かが理由を知っていますか?

4

1 に答える 1

1

これが問題かどうかはわかりませんが、あなたのケースの1つであなたがやっている:

$('#hdnSelectedModule').text(selectedVal);

次のようになります。

$('#hdnSelectedModule').val(selectedVal);

また、すでに FireBug を使用しているため、Netタブで AJAX リクエストを調べます。サーバーに送信されたすべてのキーと値のペアが表示され、問題をより簡単に特定できるようになります。たとえばSelectedModule=some_value、リクエストに a はありますか? その場合、モデルに呼び出されたプロパティがあり、単純な型 (文字列など) であると仮定すると、SelectedModuleその値を取得できます。

于 2012-12-31T12:42:18.440 に答える