2013-07-18 8 views
0

나는 다음과 같이 그 모델 인보이스 페이지에 2 드롭 다운을 갖고 싶어 첫 번째 드롭 다운의 이름으로, 해당 멤버의 주소 만 채워야합니다.EF 여러 드롭 다운

public ActionResult Create() 
    { 
     ViewBag.MemberID = new SelectList(db.Members, "MemberID", "FirstName"); 
     return View(); 
    } 

와 cshtml 파일은 다음과 같습니다 다음 InvoiceController의 생성() 메소드 코드는 사전에

@model MvcDemoApp.Models.Invoice 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) { 
@Html.AntiForgeryToken() 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>Invoice</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.MemberID, "Member") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("MemberID", String.Empty) 
     @Html.ValidationMessageFor(model => model.MemberID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.AddressID) 
    </div> 
    <div class="editor-field"> 
     @@Html.EditorFor(model => model.AddressID)@ 
     @Html.ValidationMessageFor(model => model.AddressID) 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

감사합니다.

+0

어디에서 여러 번 드롭 다운이 발생합니까? 두 번째 드롭 다운에서 주소를 입력해야하는 위치는 어디입니까? 주어진 코드를 근거로 오해의 소지가 있습니다. –

답변

0

내가 아는 한 첫 번째 드롭 다운 목록에서 선택한 멤버 이름을 기반으로 드롭 다운 목록 (또는 무엇이든)을 주소로 생성하려고합니다.

이렇게하려면 부분보기를 사용해야합니다. 첫 번째 드롭 다운 목록에 ID 을 입력하고 드롭 다운 목록이 선택되면 주소를로드하는 jquery를 사용하여 부분보기를로드합니다. 은보기에 예를 들어, 당신은이

@Html.DropDownList("MemberID", String.Empty, new{id = "MembersName") 
<div id="partialDiv"></div> 

처럼 뭔가를 할 수 당신은 당신이 함수 때 호출되는있는 컨트롤러에 GetMemberAddress라고 추가해야

<script> 

    $("#MembersName").on("change", function() { 
     var memberId = $(this).val(); 
     $.get('@Url.Action("GetMemberAddress", "Module")', { Id: memberId }, function(result) { 
      $("#partialDiv").html(result); 
     }); 

     //uncomment following section to check if the partial view is working properly 
     /*.done(function() { alert("done"); }) 
      .fail(function() { alert("fail"); }) 
      .always(function() { alert("completed"); });*/ 

    }); 

</script> 

JQuery와를 사용하여 일부 사업부를 채울 수 드롭 다운 목록 항목이 선택되면 위의 스크립트에서 다음 코드로 완료됩니다.

$.get('@Url.Action("GetMemberAddress", "Module")', { Id: memberId }, 

저는 문제를 해결하기위한 기술을 제공하고 있습니다. 원하는 결과에 따라 사용해야합니다.

+0

부분 div에서 드롭 다운을 채우는 방법도 편집 또는 만들기의 경우 ID를 저장하고 싶습니까? – newbie

관련 문제