i am having tow list boxes in a modalpopup window. In the first listbox i am storing all the availble products.the other list box will be initially empty. using couple of buttons i can pass listitems between the listboxes. my problem is when i assign some listitems to the second listbox. i cannot get them back go the controller as a part of the viewmodel i am assigning.
Following is the piece of code that i tried to use. apart from the list boxes there are some other items in the viewmodel.
View
<tr>
<td class="value" style="width: 45%; height: 75px">
<% var lst = new SelectList(Model.GetAllProducts, "ProductID", "ProductName").ToList();%>
<%: Html.ListBox("FromBox", lst, new { style = "width: 85%" })%>
</td>
<td class="label" style="width: 5%; height: 75px">
<input type="button" id="btnMoveRight" value=">>" onclick="fnMoveItems('FromBox','ToBox')" />
</td>
<td class="label" style="width: 5%; height: 75px">
<input type="button" id="btnMoveLeft" value="<<" onclick="fnMoveItems('ToBox','FromBox')" />
</td>
<td class="value" style="width: 45%; height: 75px">
<% var lst1 = new SelectList(Model.GetAssignedProducts, "ProductID", "ProductName").ToList();%>
<%: Html.ListBox("ToBox", lst1, new { style = "width: 85%" })%>
</td>
</tr>
in the Controller my viewmodel is like this
public class ProductsViewData
{
public Products Products { get; set; }
// public string PrivID { get; set; }
public List<Products> GetAllPriviliges { get; set; }
public IList<Products> GetAssignedProducts { get; set; }
}
and my create method in the controller is as follows
[HttpGet]
public ActionResult Create()
{
try
{
WorkUnit workunit = new WorkUnit();
Products _Products = new Products();
var viewModel = new ProductsViewData { Products = _Products };
viewModel.GetAllProducts = _productsService.GetAll(workunit).ToList();
viewModel.GetAssignedProducts = _svcProductsProducts.GetProductProduct(0);// binds o items to the second list box
return PartialView("Create", viewModel);
}
catch (Exception e)
{
TempData["StatusLine"] = "Error: " + e.Message;
return View();
}
}
/// <summary>
/// Add Product
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(ProductsViewData ViewModel)
{
try
{
WorkUnit workunit = new WorkUnit();
Products _Products = new Products();
_Products.EndDate = System.DateTime.Now;
_Products.StartDate = System.DateTime.Now;
_Products.CreatedDate = System.DateTime.Now;
// when i try to look for the items in the viewmodel i could not see any values for the getassignedproducts.
if (!TryUpdateModel(_Products, "Products"))
{
var viewModel = new ProductsViewData
{
Products = _Products
};
viewModel.GetAllProducts = _productsService.GetAll(workunit).ToList();
viewModel.GetAssignedProducts = _svcProductsProducts.GetProductProduct(0);
return View("Edit", viewModel);
}
//Save Product
_ProductsService.Save(workunit, ViewModel.Products);
TempData["StatusLine"] = "Added new Product ";
return RedirectToAction("List");
}
catch (Exception e)
{
TempData["StatusLine"] = "Error: " + e.Message;
return View("Edit");
}
}
the problem i am having is i am not able to understand how to pass the list of the items in the "ToBox" (second List box) assigned from " FromBox" (first list box) back to the controller.
I am new to MVC2 and fluent nhibernate.
Please let me know if there is a way to do this.