Deepak Awashti

Deepak Awashti

  • NA
  • 301
  • 74.1k

how to copy listbox data and display in another listbox MVC4

Dec 28 2015 6:13 AM
how to data copy in listbox1 and store viewbag but viewbag data display and store in listbox 2
 
My controller code ------------------
 
public ActionResult Index()
{
DataClasses1DataContext db = new DataClasses1DataContext();
List<TestTable> objlist = (from data in db.TestTables
select data).ToList();
SelectList objmodeldata = new SelectList(objlist, "ColumnID", "ColumnID");
/*Assign value to model*/
CountryModel objListmodel = new CountryModel();
objListmodel.ListModel = objmodeldata;
return View(objListmodel);
}
[HttpPost]
public ActionResult Index(string listcountry)
{
DataClasses1DataContext objentity = new DataClasses1DataContext();
List<TestTable> objlist = (from data in objentity.TestTables
select data).ToList();
TestTable objdata = new TestTable();
objdata.ColumnID = "SELECT";
// objdata.ColumnID = 0;
objlist.Insert(0, objdata);
SelectList objmodeldata = new SelectList(objlist, "ColumnID", "ColumnID");
CountryModel objcountrymodel = new CountryModel();
objcountrymodel.ListModel = objmodeldata;
ViewBag.ColumnID = objlist.Where(m => m.ColumnID ==
listcountry).FirstOrDefault().ColumnID;
return View(objcountrymodel);
}
 
View Code---------------------------------------------
 
@model MvcLIstBoxTask.Models.CountryModel
@{
ViewBag.Title = "Bind ListBox in MVC Using C#.Net";
}
@using (Html.BeginForm("Index", "Home"))
{
<h2>Bind ListBox</h2>
@Html.ListBox("listcountry", Model.ListModel, new { @style = "width:200px;height:100px;" })
<br /><br />
<input type="submit" value="Submit" />
<br />
<div>
Selected list :@ViewBag.ColumnID
</div>
}
 
please help me any one ......  

Answers (10)