Solved

mvc textbox add value to listbox

Posted on 2013-06-25
6
1,563 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
[X]
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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 42
What are the simplest ways to create a Merge Sort? 3 37
Code works but breaks when I add one section 4 30
ModalPopup  question 22 39
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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