2013-01-18 53 views
0

我有一個表格,其中包含一些填充數據的文本。我想在下拉列表更改後填寫文本框中的數據。下拉列表中的值改變後,Mvc3填充數據

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

我不能夠在填寫表格的數據。我不明白爲什麼我無法填寫數據。腳本或我的代碼中是否有任何更改?是否有任何示例在dropdownlist值更改後填充數據?

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

我已經試過在一個示例項目類似的事情。在這裏,我可以獲取數值並在文本框中顯示,但是我在同一個視圖上添加了一個更多視圖,每次我從下拉屏幕截圖中選擇OS

Screenshot

+0

你能看到$ .ajax中的'success'響應嗎? –

+0

檢查在AJAX調用觸發時得到的響應。 AJAX調用是否觸發?如果可以的話,在成功和錯誤回調中添加'console.log',這可能會帶來更多的清晰度。 – Christoffer

回答

0

大家好我已經用這個解決了我的問題。沒有必要創建任何javascript。我已經解決這個問題,而不使用JavaScript。

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

在您發佈可能是一個缺少代碼唯一的瑕疵;

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

小心,你可能會開始偉大的分號辯論;)但不,這不可能是問題。 JavaScript具有自動分號插入,所以該行應該是有效的。 – Christoffer

+0

我已經嘗試過,但不起作用。 –

+0

@ fuchs777我編輯了我的問題。你能看看嗎? –