Tuesday, 15 April 2014

c# - How to get selected value from @Html.DropDownList -



c# - How to get selected value from @Html.DropDownList -

the project .net framework3.5 , mvc framework 1.0

in view have dropdown

@html.dropdownlist("prodlist",@model.products)

a add together product button

<td> <input type ="button" name ="add product" value ="add product" id="button1" /> </td>

and textbox @html.textbox ("prodselected")

when click add together product button should able display value of item selected on dropdownlist in textbox "prodselected"

please can 1 allow me know how it. if there alter needed in existing code please suggest.

the view is:

<%@ page language="c#" inherits="system.web.mvc.viewpage<mockbdpworkflowtestapp.viewmodels.workflowtestviewmodel>" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="head1" runat="server"> <title>submission</title> </head> <body> <div> <% using (html.beginform()) { %> <div> <table> <tr> <td> <div id="sid"> <table id="tblid" cellpadding ="0" cellspacing ="0" border="1"> <tr> <td width ="50%"> <label for="process inastance id"> process inastance id: </label> &nbsp;&nbsp; <%=@html.textbox("pinstance",@model.processinstance) %></td> <td width ="50%"> <label ="mail id">mail id: </label> &nbsp;&nbsp; <%=@html.textbox("mail",@model.mail) %> </td> </tr> </table> </div> <br /><br /> <table id="tbldet" cellpadding ="0" cellspacing ="0" > <tr> <td width="50%"> <label for="submission number"> submission number: </label> &nbsp;&nbsp; <%=@html.textbox("sno") %></td> <td width ="50%"> <label ="name insured">name insured: </label> &nbsp;&nbsp; <%=@html.textbox("nameinsured") %> </td> </tr> <tr> <td width="50%"> <label for="broker code"> broker code: </label> &nbsp;&nbsp; <%=@html.textbox("brokercode") %></td> <td width ="50%"> <label ="broker name">broker name: </label> &nbsp;&nbsp; <%=@html.textbox("brokername") %> </td> </tr> <tr> <td width="50%"> <label for="product code"> product code: </label> &nbsp;&nbsp; <%=@html.dropdownlist("prodlist",@model.products)%></td> <td> <input type ="button" name ="add product" value ="add product" id="button1" /> </td> </tr> <tr> <td> <label for="product code"> product code selected: </label> &nbsp;&nbsp; <%=@html.textbox ("prodselected")%></td> <td> <input type ="submit" value ="submit submission" /> </td> </tr> </table> </td> </tr> </table> </div> <% } %> </div>

controller is:

namespace mockbdpworkflowtestapp.controllers { public class workflowtestcontroller : controller { // // get: /workflowtest/ public actionresult opensubmission(string processid, string mailid) { var submissionmodelview = new mockbdpworkflowtestapp.viewmodels.workflowtestviewmodel{processinstance =processid, mail service =mailid} ; submissionmodelview.products = new list<selectlistitem>(); submissionmodelview.products.add(new selectlistitem { text = "gl", value = "0", selected = true }); submissionmodelview.products.add(new selectlistitem { text = "property", value = "1" }); submissionmodelview.products.add(new selectlistitem { text = "package", value = "2" }); submissionmodelview.products.add(new selectlistitem { text = "island marine", value = "3" }); homecoming view("submission", submissionmodelview); } } }

the viewmodel is:

namespace mockbdpworkflowtestapp.viewmodels { public class workflowtestviewmodel { public string processinstance { get; set; } public string mail service { get; set; } public list<selectlistitem> products; } }

to view added jquery function below:

<%@ page language="c#" inherits="system.web.mvc.viewpage<mockbdpworkflowtestapp.viewmodels.workflowtestviewmodel>" %> <script src="/scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#button1').click(function() { $('#prodlist').val($('#prodselected').val()); }); }); </script>

there no problem function prodlist not show value instead alert gives me junk jquery : tried this: $('#prodlist option:selected').text; not have selected value prodlist too, , instead alert gives me junk jquery .

try this:

create property in viewmodel store selected value dropdownlist

model public class workflowtestviewmodel { public string processinstance { get; set; } public string mail service { get; set; } public int product { get; set; } public list<selectlistitem> products; } view <td> <label for="product code">product code: </label> @html.dropdownlistfor(m => m.product, model.products)</td> <td> controller [httppost] public actionresult opensubmission(workflowtestviewmodel model) { //model.product dropdown value //your codegoes here }

here fiddle

c# asp.net-mvc

No comments:

Post a Comment