繁体   English   中英

如何使用select2 ASP.NET MVC传递2值

[英]how to pass 2 value with select2 ASP.NET MVC

我有1个使用Select2的下拉列表,

我希望结果具有2个值,ID和电子邮件

这是我的控制器

public ActionResult Input(string input, string email)
    {

        // Populate your ViewModel to store each of those things
        var model = new CostVM()
        {
            error = input,
            email = email,
        };
        ViewData["List"] = new SelectList(db.depts, "id", "jenis");
        return View(model);
    }

    [HttpPost]
    public ActionResult Input(CostVM model)
    {
        FormCollection a = new FormCollection(Request.Unvalidated().Form);

        var mail = db.depts.ToList();
        model.input = string.Join(",", model.selec);

        int ttd = 0;

        for (ttd = 0; ttd < model.selec.Count(); ttd++)
        {
            var oke = db.depts.Find(Convert.ToInt32(model.selec[ttd]));
            string[] idList = oke.email.Split(new char[] { ',' });
            model.email = string.Join(",", idList);
        }


        return RedirectToAction("Input", "Select", new { input = model.input, email = model.email });
    }

我的问题是,如果我添加2个或更多选项,则只有一个添加到数据库

有人可以修复我的代码吗? 对不起,我的英语不好

替换model.email = string.Join(“,”,idList); 使用model.email + = string.Join(“,”,idList);

为了在Entity Framework中进行任何更改,您需要使用上下文类的SaveChanges方法(从DbContext类派生的DbContext )。 我在您的代码中没有看到这样的命令(我想对象db是上下文类的)。 因此,我建议您先尝试一下。

我认为您应该放置db.SaveChanges(); db.SaveChangesAsync(); 在您的HTTP POST版本的Input方法中,就在返回之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM