asp.net mvc - Get selected DropDownList item from MVC Partial view -


i use @html.dropdownlist in partial view show list of banks , render in bank branch view. how can selected bank in @html.dropdownlist , fill bankid in bankbranchmodel .

here code:

bankbranchmodel :

public class bankbranchmodel : basemodel {     public int? **bankid** { get; set; }     public string name { get; set; }     public string code { get; set; }     public string phone { get; set; }     public string address { get; set; } } 

my partial view :

@html.dropdownlist("banks", model , "- please select -"}) 

bankbranch view:

@model mvcfirstsample.models.bankbranchmodel @{     viewbag.title = "create"; }  <h2>create</h2>  @using (html.beginform()) {     @html.validationsummary(true)       <div class="editor-field">         **@html.action("getallbank", "bankbranch")**      </div> 

getallbanks return partial view:

public actionresult getallbank()         {              var banks = context.banks.tolist();             list<banklookup> banklookuplist = new list<banklookup>();              var listitems = new list<selectlistitem>();              foreach (var bank in banks)             {                 listitems.add(new selectlistitem { text = bank.name, value = bank.id.tostring()});             }              **return partialview("banklookup", listitems);**         } 

why don't want use viewbag? in partial view can say:

@html.dropdownlist("listitems",(ienumerable<selectlistitem>)viewbag.listitems ) 

, use jquery selected item:

      var selectbanks= $("#listitems");          var selectedoptionid = selectbanks.find("option:selected").val(); 

then populate input bankid assuming has id of bankid:

     $("#bankid").val(selectedoptionid); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -