Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

mvc textbox add value to listbox

Posted on 2013-06-25
6
1,534 Views
Last Modified: 2013-06-27
does anyone have example not on the client side but in the controller and model to add a value from a text box to a listbox.
0
Comment
Question by:sevensnake77
  • 4
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39276806
Within the controller there are no list boxes or text boxes. You only have what was passed to the controller and what you put in the model. You basically need to create a list of items that your model will contain, and then append the incoming value to the model's list. Then spit that list out in the view.

e.g.

Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _28167867.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View(new YourModel());
        }

        [HttpPost]
        public ActionResult Index(string textBoxValue, List<string> listBoxValues)
        {
            YourModel model = new YourModel();

            if (listBoxValues == null) listBoxValues = new List<string>();

            listBoxValues.Add(textBoxValue);

            model.TextBoxValue = textBoxValue;
            model.ListBoxValues = listBoxValues;

            return View(model);
        }
    }

    public class YourModel
    {
        public YourModel()
        {
            this.TextBoxValue = string.Empty;
            this.ListBoxValues = new List<string>();
        }

        public string TextBoxValue { get; set; }
        public List<string> ListBoxValues { get; set; }
    }
}

Open in new window


View
@model _28167867.Controllers.YourModel

@{
    ViewBag.Title = "Home Page";
}
@{Html.BeginForm();}
<label for="textBoxValue">TextBox Value:</label>
<input type="text" name="textBoxValue" value="@Model.TextBoxValue" />
<label for="listBoxValues">ListBox Values:</label>
<select name="listBoxValues" multiple="multiple">
@foreach (var item in Model.ListBoxValues)
{
    <option>@item</option>
}
</select>
<input type="submit" value="Submit" />
@{Html.EndForm();}

Open in new window


Be mindful that in the above only the selected values of a <select> list are transmitted back to the server. If you need to persist the entire list, then you will need to do so on the server, and repopulate your model each time in addition to adding the new element.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39279651
error (Object reference not set to an instance of an object.)
<option>@item</option>
}
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39279724
ok I tested yours in a totally separate app. But It does not append each every time I try to add another value the last value in the list box diss-appear. This is why we should do it on the client side then add it . correct me if I am wrong.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39279806
But It does not append each every time I try to add another value the last value in the list box diss-appear.

Hence my comment above:

Be mindful that in the above only the selected values of a <select> list are transmitted back to the server. If you need to persist the entire list, then you will need to do so on the server, and repopulate your model each time in addition to adding the new element.

If you want to keep the same values in the list while adding new ones, then you have to persist the list on the server side. Update the list in code-behind by adding in the new item. Make sure this updated list is persisted on the server. Then spit the updated list out in the view.

Your only other alternative, AFAIK, would be to insert hidden fields into your view that would get posted back to the server on each request.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39280027
I think its fair to say, Add it on the client side using Json data or knockjs. I have entire form besides the textbox and listbox. Posting back to the server is a little to much for each value. I will just add all the values in mem then send it all at once to the server.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 39281640
tks for your insight.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question