50956

dropdownlist selection returning null @ mvc4

Question:

I am trying to insert to database from view page which has dropdownlist , textbox's .. when i enter something and click on save means i am getting nothing from dropdown selection which is binded .

My code : @model IEnumerable<iWiseapp.Models.LObmodel> @using (@Html.BeginForm("stop", "Home", FormMethod.Post)) { @Html.DropDownList("Data",ViewBag.Data as SelectList,"select a sdsd",new {id="LOB_ID"}) @Html.DropDownListFor("sss",new SelectList(Model,"lob_id","lob_name")) , @Html.DropDownList("LObmodel", new SelectList(ViewBag.data ,"Value","Text")) @Html.DropDownListFor(model => model.lob_name, new SelectList(ViewBag.Titles,"Value","Text"))

I tried above all possibilities but nah i am confused nothing working out

ADDED MY CONTROLER CODE

[HttpGet] public ActionResult stop() { ServiceReference1.Service1Client ser_obj = new ServiceReference1.Service1Client(); IEnumerable<LobList> obj = ser_obj.GetData(); //i am Getting list of data through WCF from BUSINESS LAYER WHERE i created entities via EF List<SelectListItem> ls = new List<SelectListItem>(); foreach (var temp in obj) { ls.Add(new SelectListItem() { Text = temp.LOB_NAME, Value = temp.LOB_ID.ToString() }); } //then create a view model on your controller and pass this value to it ViewModel vm = new ViewModel(); vm.DropDown = ls; // where vm.DropDown = List<SelectListItem>(); THE COMMENTED CODE BELOW IS WHAT I AM DOING //var mode_obj = new List<LObmodel>(); //Created LOBmodel class in model which is excat same of entities in Business class //var jobList = new List<SelectListItem>(); //foreach (var job in obj) //{ // var item = new SelectListItem(); // item.Value = job.LOB_ID.ToString(); //the property you want to display i.e. Title // item.Text = job.LOB_NAME; // jobList.Add(item); //} //ViewBag.Data = jobList; return View(jobList); or return view (obj) }

Any expert advice is appreciated

MY FIELDS , IS THESE PERFECT

public class template { public List<LobList> LOBs { get; set; } //LOBLIST FROM Entities in business layer public int selectedLobId { get; set; } public LobList SelectedLob { get { return LOBs.Single(u=>u.LOB_ID == selectedLobId) ;} } } AND public class LObmodel { public int LOB_ID { get; set; } public string LOB_NAME { get; set; } }

Answer1:

I would recommend putting the selectlist into your model instead of passing it through the view bag. The option you want is

@Html.DropDownListFor(model => model.lob_name, new SelectList(ViewBag.Titles,"Value","Text"))

you can set the selected item by setting model.lob_name on the controller and on post back that value will be set to the selected value of the dropdown

on your controller you can build the list like this

List<SelectListItem> ls = new List<SelectListItem>(); foreach(var temp in model){ //where model is your database ls.Add(new SelectListItem() { Text = temp.Text, Value = temp.Value }); } //then create a view model on your controller and pass this value to it LObmodel vm = new LObmodel(); vm.DropDown = ls; // where vm.DropDown = List<SelectListItem>(); return View(vm);

then on your view you can reference that

@Html.DropDownListFor(x => x.lob_name, Model.DropDown)

with your model add the select list

public class LObmodel { public int LOB_ID { get; set; } public string LOB_NAME { get; set; } public List<SelectListItem> DropDown { get; set; } }

then the top of your view would be

@model LObmodel

Answer2:

I had the same problem . But i changed the view code of DDL using this code :

@Html.DropDownListFor(x => x.ClassID, (SelectList)ViewBag.ClassName);

The dropdownlist will bind to your model class called ClassID You will not be able to post the textual value of the ddl to the controller, only the ID behind the ddl.

Recommend

  • PHP: CodeIgniter; Managing two db connections; variable database parameters
  • Call Go function that accepts a slice of interface A with a slice of struct B (B implements A)
  • Laravel full URL routing
  • FOSRestBundle “avoid” Javascript eventListener
  • Passing the argument name while calling function in javascript
  • Null Pointer Exception: HTTP Entity
  • Asp Core, How to create Paging?
  • Finding regular expressions for languages otherwise described
  • Javascript get plain text from server
  • Writing a recursive function on lists in Haskell
  • Generating anchors with PyYAML.dump()?
  • Django model for a Postgres view
  • Activation Function choice for Neural network
  • How to getText() from the input field of an angularjs Application
  • Combining two different ActiveRecord collections into one
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Django invalid literal for int() with base 10
  • How does this usort cmp function actually work?
  • Eloquent update method change created_at timestamp
  • How to define and use opencv mat of user type
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Cassandra Data Model
  • Matrix multiplication with MKL
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • File not found error Google Drive API
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?