如何使用mvc中的模型绑定器将(视图)列表框中的数据传输到控制器 [英] how to transfer data in(view) listbox to the controller using model binder in mvc

查看:134
本文介绍了如何使用mvc中的模型绑定器将(视图)列表框中的数据传输到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我找到这个...

Can some one help me to find this...

推荐答案

@model TestMVC4.Controllers.TestString
@{
    ViewBag.Title = "Transfer";
}


@using (Html.BeginForm("redirectObject", "Collection", Model.myFriendList))
{ <h2>Transfer</h2>
 
@Html.ListBoxFor(s=>s.myFriendList, new SelectList(Model.myFriendList))
    
  <button  type="submit" value="test" name="btnSubmit"></button>
  }










 public class CollectionController : Controller
    {
        //
        // GET: /Collection/

        public ActionResult Index()
        {
            TestString MyListBoxFriends = new TestString();
            List<string> friends = new List<string>();
            friends.Add("Himanshu");
friends.Add("Sumit");
friends.Add("Jo"); 
friends.Add("Waan");
            MyListBoxFriends.myFriendList = friends;
            return View("Transfer", MyListBoxFriends);
        }
        [HttpPost]
        public ActionResult redirectObject(TestString cc)
        {
            var yourFriendList = cc.myFriendList;
            return View("YourView");
        }
    }
//this is model class which is bind to view
    public class TestString
    {
        public List<string> myFriendList { get; set; }
    } 
}


这篇关于如何使用mvc中的模型绑定器将(视图)列表框中的数据传输到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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