0

データを入力するために提出されたテキストを含むフォームがあります。ドロップダウンリストを変更した後、テキストボックスにデータを入力したい。

MyView.chstml

@model BloodBank.Models.NewCamp

@{
  ViewBag.Title = "New Camp";
  Layout = "~/Views/Shared/_Layout - Menu.cshtml";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script type="text/javascript">
$(function () {
    $("select#OrganisationID").change(function (evt) {

        if ($("select#OrganisationID").val() != "0") {

            $.ajax({
                url: "GetOrganizationInfo?orgID=" + $("select#OrganisationID").val(),
                type: 'POST',
                data: { OS: $("select#OrganisationID").val() },
                success: function (response) {
                    $("select#OrganisationID").replaceWith(response)
                },
                error: function (xhr) {
                    alert("Something went wrong, please try again");
                }
            });
        }
    });
});
</script>


 @using (Html.BeginForm())
 {
@Html.ValidationSummary(true, "New Camp creation was unsuccessful. Please correct the errors and try again.")
    <div>
    <table style="border-style:none;border-width:0;border:0;">
        <tbody>
        <tr>
            <td style="border:0;vertical-align:middle;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.OrganisationID)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">

                     @Html.DropDownListFor(m => m.OrganisationID, (SelectList)ViewBag.OrganisationList)&nbsp; &nbsp; &nbsp; &nbsp;

                    @* <select id="Area">                            
                        @foreach (var arearow in (SelectList)ViewBag.OrganisationList)
                        {
                            <option value="@arearow.Value">@arearow.Text</option>                                
                        }
                    </select>*@

                    @Html.ActionLink("Add New Organisation", "AddOrganisation", "Organisation", null, null)
                </div>
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.ValidationMessageFor(m => m.OrganisationID)
                </div>
            </td>
        </tr>

        <tr>
            <td style="border:0;text-align:left;" colspan="2"> <h3>Contact Person Information</h3></td>
        </tr>

        <tr>
            <td style="border:0;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.Email)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.Email)&nbsp;&nbsp;&nbsp;
                    @Html.ValidationMessageFor(m => m.Email)
                </div>
            </td>
        </tr>

        <tr>
            <td style="border:0;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.FirstName)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.FirstName)&nbsp;&nbsp;&nbsp;
                    @Html.ValidationMessageFor(m => m.FirstName)
                </div>
            </td>
        </tr>

        <tr>
            <td style="border:0;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.LastName)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.LastName)&nbsp;&nbsp;&nbsp;
                    @Html.ValidationMessageFor(m => m.LastName)
                </div>
            </td>
        </tr>

        <tr>
            <td style="border:0;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.Phone)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.Phone)&nbsp;&nbsp;&nbsp;
                    @Html.ValidationMessageFor(m => m.Phone)
                </div>
            </td>
        </tr>

        <tr>
        <td colspan="2" style="border:0;text-align:center;">

        </td>
        </tr>
        </tbody>
    </table>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;
    <input type="submit" value="Submit" id="ClickMe" class="cssLoginButton blue"/>
</div>
}

私の行動

    [Authorize]
    [OutputCache(Location = OutputCacheLocation.None)]
    public ActionResult NewCamp()
    {
            var user = (BloodBank.Security.BloodBankMembershipUser)Membership.GetUser();
            this.BindOrganisations(user.BloodBankID);
            return View();        
    }

    public ActionResult GetOrganizationInfo(string orgID)
    {
        if (!string.IsNullOrEmpty(orgID))
        {
            var model = (new UserManager()).GetCampPersonOrganisationDetailsByOrganisationID(orgID);
            Models.NewCamp newcampModel = new Models.NewCamp();

            if (model.Count > 0)
            {
                newcampModel.CampID = model[0].CampID;
                newcampModel.Organisation = "";
                newcampModel.OrganisationID = model[0].OrganisationID;
                newcampModel.FirstName = model[0].FirstName;
                newcampModel.LastName = model[0].LastName;
                newcampModel.Email = model[0].Email;
                newcampModel.Phone = model[0].Phone;

                var organisations = this.GetOrganisations(model[0].BloodBankID);
                if (organisations != null)
                    ViewBag.OrganisationList = new SelectList(organisations, "OrganisationID", "NameCity");
            }

                return View("NewCamp", newcampModel);
        }
        else
            return View();
    }

フォームにデータを入力できません。データを入力できない理由がわかりません。スクリプトまたはコードに変更はありますか?ドロップダウンリストの値が変更された後にデータを入力する例はありますか?

- - - - - アップデート - - - - - -

サンプルプロジェクトで同様のことを試しました。ここで値を取得してテキストボックスに表示できますが、下のスクリーンショットのようにドロップダウンからOSを選択するたびに、同じビューにもう1つのビューが追加されます。

スクリーンショット

4

2 に答える 2

0

あなたが投稿したコードの唯一の欠陥は欠落している可能性があります;

success: function (response) {
    $("select#OrganisationID").replaceWith(response);
},
于 2013-01-18T13:01:37.637 に答える
0

みなさん、こんにちは。これを使って問題を解決しました。javascriptを作成する必要はありません。私はjavascriptを使用せずにこれを解決しました。

@Html.DropDownListFor(m => m.OrganisationID, (SelectList)ViewBag.OrganisationList, new { onchange = "document.location.href = 'NewCamp?orgID=' + this.options[this.selectedIndex].value;" })
于 2013-01-19T06:51:54.343 に答える