Solved

mvc textbox add value to listbox

Posted on 2013-06-25
6
1,482 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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

20 Experts available now in Live!

Get 1:1 Help Now