【问题标题】:how to pass 2 value with select2 ASP.NET MVC如何使用 select2 ASP.NET MVC 传递 2 个值
【发布时间】:2018-01-25 17:45:40
【问题描述】:

我有 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 个或更多选项,则只有一个添加到数据库中

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    替换 model.email = string.Join(",", idList); 和 model.email += string.Join(",", idList);

    【讨论】:

      【解决方案2】:

      为了在实体框架中进行任何更改,您需要使用上下文类的SaveChanges 方法(派生自DbContext 类的方法)。我在您的代码中没有看到这样的命令(我猜对象 db 属于上下文类)。所以,我建议先尝试一下。

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

      【讨论】:

      • 不,先生,我的问题是为什么我不能为 2 行创建 2 个数组,对于数组 ID,使用值 1,2,3,4 完成,但是对于数组电子邮件,仅从 id 1 添加了 1 个,并且来自 id 2,3,4 未添加 @MichalTurczyn
      猜你喜欢
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多