Click here to Skip to main content
15,120,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
(prob is that- when i click to submit button mine output is going to some other page but i want on the same page ,i need a jscript functn in view so that it holds the data of 2nd listbox nd return it be4 the load of page(with popup would help me more..))

My Code is as follows:-




//Model
XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;



namespace Listbox2.Models
{
    public class namesviewmodel
    {
        public IEnumerable<SelectListItem> Names { get; set; }
        public IEnumerable<string> SelectedNames { get; set; }
    }
}



//HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Listbox2.Models;
using System.Text;

namespace Listbox2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            odaEntities1 db = new odaEntities1();
                    
            List<SelectListItem> listSelectListItem = new List<SelectListItem>();
            foreach (Names name in db.Names)
            {
                SelectListItem selectListItem = new SelectListItem()
                {
                    Text = name.Fname,
                    //Value = name.noid.ToString(),

                };
                listSelectListItem.Add(selectListItem);
            }
            namesviewmodel Namesviewmodel = new namesviewmodel();
            Namesviewmodel.Names = listSelectListItem;
            return View(Namesviewmodel);
        }


        [HttpPost]
        public string Index(IEnumerable<string>Names)
             //public string Index(IEnumerable<string> SelectedNames, IEnumerable<string> Names)
        {
            if (Names == null)
            {
               return "you did not selected any name";
                         
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("You Selected - " + string.Join(",", Names));
                return sb.ToString();
                //return"You Selected - "+ string.Join(",",Names);
               

              }
            
        }
    }
}


and view is:-
@model Listbox2.Models.namesviewmodel
@{
    ViewBag.Title = "Index";


<script language="javascript" type="text/javascript">

    function MoveItem() 
    {
       
            $("#SelectedNames option:selected").appendTo("#Names");

    }
    function MoveBackItem() {
        $("#Names option:selected").appendTo("#SelectedNames");

    }
   

</script>
}

Names


<div style="font-family:Arial">

@using (@Html.BeginForm())
{
     @Html.ListBoxFor(x => x.SelectedNames, Model.Names)
<input id="btnAdd" type="button" value=" >> " onclick="MoveItem();" />
<input id="btnBack" type="button" value=" << " onclick="MoveBackItem();" />
    // @Html.ListBoxFor(m => m.SelectedNames, new MultiSelectList(Model.Names))

@Html.ListBoxFor(model => model.Names, new MultiSelectList(""))
<br />
<input id="SelectedNames" type="submit"value="Submit"/>
    
}
</div></string></string></string>
Posted
Updated 5-Dec-13 21:59pm
v2
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900