Solved

mvc textbox add value to listbox

Posted on 2013-06-25
6
1,500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now