c# - ASP.NET MVC Binding Value in View -


struggling value drop down, passed controller db inserting. starts simple class each item in drop down.

 public class selectlisturgencynum {     public string text { get; set; }     public string value { get; set; } } 

then in view model create list of these objects such..

public class createviewmodel {     public change changevm { get; set; }      public list<requesttype> rtypes { get; set; }      public list<selectlisturgencynum> listurgency { get; set; } } 

finally controller populate list displayed in dropdown.

public actionresult create()     {          var urgnums = new list<selectlisturgencynum>();         for(int = 1; < 6; i++)         {             urgnums.add(new selectlisturgencynum() { text = i.tostring(), value = i.tostring() });         }          var viewmodel = new createviewmodel         {             listurgency = urgnums          };            return view(viewmodel);     } 

i used 1 - 5 make simpler understand.

my view creates ddl this.

  <div class="form-group">         @html.labelfor(model => model.changevm.urgencynum, htmlattributes: new { @class = "control-label col-md-2" })         <div class="col-md-2">             <select class="form-control" id="select">                 <option> 1 = least urgent</option>                 @foreach(var item in model.listurgency)                 {                     <option>@item.text</option>                 }                 </select>              @*how grab value of selected??*@           @html.validationmessagefor(model => model.changevm.urgencynum, "", new { @class = "text-danger" })          </div> 

so i'm not sure how value of selected drop down option is, on insert can access it. possibly hiddenfor?

and if use hidden for, how look?

if answered please link..

if want pass text , value, can use selectlistitem class system.web.mvc, there passing items dropdown lists.

public class createviewmodel {     public change changevm { get; set; }      public list<requesttype> types { get; set; }      public list<selectlistitem> listurgency { get; set; } } 

in controller create() method create new changevm also

var viewmodel = new createviewmodel() {     changevm = new change(),     listurgency = urgnums };  return view(viewmodel); 

on view can use html.dropdownlistfor extension method take care of creating list given items , proper name.

html.beginform take care of creating form proper "action" url, , method.

@using (html.beginform("create", "home", formmethod.post)) { <div class="form-group">     @html.labelfor(model => model.changevm.urgencynum, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-2">         @html.dropdownlistfor(model => model.changevm.urgencynum, model.listurgency, "choose urgency...")         @html.validationmessagefor(model => model.changevm.urgencynum, "", new { @class = "text-danger" })     </div>     <input type="submit" value="submit"/> </div> } 

in controller can use

    [httppost]     public actionresult create(createviewmodel vmodel)     {         if(vmodel?.changevm?.urgencynum != null)         {             var chosenvalue = vmodel.changevm.urgencynum;         }          return view(vmodel);     } 

if goes well, should have value chosen in vmodel.changevm.urgencynum


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -