如何将数据从一个列表框移动到MVC4中的另一个列表框 [英] how to move data from one listbox to another listbox in MVC4

查看:60
本文介绍了如何将数据从一个列表框移动到MVC4中的另一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(问题是 - 当我点击提交按钮我的输出将转到其他页面,但我想在同一页面上,我需要一个jscript functn在视图中,以便它保存第二个列表框的数据nd返回它be4页面的负载(弹出窗口会帮助我更多..))



我的代码如下: -









//型号

(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

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>

推荐答案

(#SelectedNames option:selected)。appendTo(#Names);

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


(#Names option:selected)。appendTo(#SelectedNames);

}


< / script>
}
("#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>


这篇关于如何将数据从一个列表框移动到MVC4中的另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆