Solved

mvc textbox add value to listbox

Posted on 2013-06-25
6
1,517 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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