下拉列表值更改后,Mvc3填充数据

时间:2013-01-18 10:55:48

标签: c# javascript asp.net-mvc-3 razor

我的表单中包含一些填写数据的文本。我想在下拉列表更改后填写文本框中的数据。

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();
    }

我无法填写表格中的数据。我不知道为什么我无法填充数据。脚本或代码中是否有任何更改?在下拉列表值更改后是否有任何填充数据的示例?

---------更新------------

我在示例项目上尝试了类似的事情。在这里,我可以获取值并显示在文本框中,但每次从下拉列表中选择操作系统时,我会在同一查看上再添加一个视图,如下面的屏幕截图所示。

Screenshot

2 个答案:

答案 0 :(得分:0)

您发布的代码中唯一的缺陷可能是缺失;

success: function (response) {
    $("select#OrganisationID").replaceWith(response);
},

答案 1 :(得分:0)

大家好我已经用这个解决了我的问题。无需创建任何 javascript 。我没有使用javascript解决了这个问题。

@Html.DropDownListFor(m => m.OrganisationID, (SelectList)ViewBag.OrganisationList, new { onchange = "document.location.href = 'NewCamp?orgID=' + this.options[this.selectedIndex].value;" })