C# dropdownlist值更改后的Mvc3填充数据
我有一个表格,其中包含一些填写数据的文本文件。我想在dropdownlist更改后在文本框中填充数据 MyView.chstmlC# dropdownlist值更改后的Mvc3填充数据,c#,javascript,asp.net-mvc-3,razor,C#,Javascript,Asp.net Mvc 3,Razor,我有一个表格,其中包含一些填写数据的文本文件。我想在dropdownlist更改后在文本框中填充数据 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")"
@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)
@* <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)
@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)
@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)
@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)
@Html.ValidationMessageFor(m => m.Phone)
</div>
</td>
</tr>
<tr>
<td colspan="2" style="border:0;text-align:center;">
</td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit" id="ClickMe" class="cssLoginButton blue"/>
</div>
}
我无法在表格中填写数据。我不明白为什么我不能填写数据。脚本或代码中是否有任何更改?dropdownlist值更改后是否有填充数据的示例
---------更新------------
我在一个示例项目上尝试过类似的事情。在这里,我可以获取值并显示在文本框中,但每次从下拉列表中选择操作系统时,我都会在相同的视图上添加一个视图,如下面的屏幕截图所示
您发布的代码中唯一的缺陷可能是缺少一个
success: function (response) {
$("select#OrganisationID").replaceWith(response);
},
大家好,我已经用这个解决了我的问题。无需创建任何javascript。我没有使用javascript就解决了这个问题
@Html.DropDownListFor(m => m.OrganisationID, (SelectList)ViewBag.OrganisationList, new { onchange = "document.location.href = 'NewCamp?orgID=' + this.options[this.selectedIndex].value;" })
您能在$.ajax中看到
success
responce吗?检查ajax调用触发时得到的响应。AJAX调用fire吗?如果可以的话,在成功和错误回调中添加console.log
,这可能会带来更清晰的信息。小心,你可能会开始伟大的分号辩论;)但不,这不太可能是问题所在。JavaScript自动插入分号,因此该行应该是有效的。@fuchs777我已经编辑了我的问题。你能看一下吗?
@Html.DropDownListFor(m => m.OrganisationID, (SelectList)ViewBag.OrganisationList, new { onchange = "document.location.href = 'NewCamp?orgID=' + this.options[this.selectedIndex].value;" })