Ihsan
Ihsan

Reputation: 35

how to pass 2 value with select2 ASP.NET MVC

I have 1 dropdownlist using Select2,

I want that the result have 2 value, ID and Email

this fot my controller

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 });
    }

My problem is, if I add 2 or more option, only one added to database

Can someone fix my code? Sorry for my bad english

Upvotes: 0

Views: 485

Answers (2)

Planet-Zoom
Planet-Zoom

Reputation: 1185

replace model.email = string.Join(",", idList); with model.email += string.Join(",", idList);

Upvotes: 2

Michał Turczyn
Michał Turczyn

Reputation: 37460

In order for any changes to take place in Entity Framework, you need to use SaveChanges method of context class (the one that derives from DbContext class). I don't see such command in your code (I guess object db is of context class). So, I would recommend trying that first.

I think you should place db.SaveChanges(); or db.SaveChangesAsync(); in your HTTP POST version of Input method, just before return.

Upvotes: 1

Related Questions