Sunday, 15 February 2015

c# - Trying to create a dropdown list and textbox, setting list item as data and text box as value - asp.net mvc 4 -



c# - Trying to create a dropdown list and textbox, setting list item as data and text box as value - asp.net mvc 4 -

i trying create search form searches based on server name or printer name. here snippet controller:

list<selectlistitem> items = new list<selectlistitem>(); items.add(new selectlistitem { text = "server name", value = "servername" }); items.add(new selectlistitem { text = "printer name", value = "printername" }); viewdata["newlist"] = items;

here view (which know wrong because doesn't work):

@using (html.beginform("search", "printqueues", formmethod.get)) { <fieldset> <legend> search </legend> @html.dropdownlist("newlist",viewdata["newlist"] selectlist) @html.textbox("newlist") <p> <input type="submit" value="submit" /> </p> </fieldset> }

if pick "server name" , set in value (such "myservernaem" textbox, want url show:

/search?servername=myservername

i'm pretty sure both controller , view incorrect.

not huge fan of approach... http works sending value each input controller. values receive in controller, should able homecoming appropriate page , information.

so if re-named drop downwards list searchtype , textbox searchcriteria you'd have nice query string like: /search?searchtype=printer&searchcriteria=epson. in controller should able receive these , homecoming appropriate page (whether printer or server).

public actionresult search(string searchtype, string searchcriteria) { if(searchtype == "printername") { // search printers using searchcriteria , homecoming appropriate view } else if(searchtype == "servername") { // search servers using searchcriteria , homecoming appropriate view } }

if go approach create enum called searchtype , utilize instead of string, allow do: if(searchtype == searchtype.printer) ...

if want go approach, values inputs when seek search , append url:

@html.dropdownlist("searchtype",viewdata["newlist"] selectlist) @html.textbox("searchcriteria") <button type="button" onclick="gotosearch();">search</button> function search() { // assumption of jquery (use document.getelementbyid otherwise...) var type = $('#searchtype').val(); var searchcriteria = $('#searchcriteria').val(); window.location = '@url.action("search", "printqueues")' + '?' + type + '=' + searchcriteria; }

happy reply questions.

c# asp.net razor asp.net-mvc-4

No comments:

Post a Comment