0

I have problem binding data in dropdown for adding data in grid. I want to pass airline name in the agent column. The dropdown value can't be changed How do I change the value of dropdown. Only the first index airline is shown in the dropdown.

Controller:

public ActionResult Create()
{
    ViewBag.AID = db.Airlines.ToList();
    return View();


}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FormCollection frmcoll, ICollection<string> hddrowpindex)
{
    foreach (var row in hddrowpindex)
    {
        InternationalArrival stock = new InternationalArrival();
        stock.AgentName = How to take selected textvalue Name from dropdown???;
        stock.AgentCode = (frmcoll["AgentCode-" + row]).ToString();
        stock.ArrivalDate = DateTime.ParseExact(frmcoll["ArrivalDate-" + row], "yyyy-MM-dd", null);
        stock.ForPAX = Convert.ToInt32(frmcoll["ForPAX-" + row]);
        stock.IndPAX = Convert.ToInt32(frmcoll["IndPAX-" + row]);
        db.InternationalArrivals.Add(stock);
        db.SaveChanges();
    }
    return RedirectToAction("Index");
}

View:

   var sectorlist=(List<CompetitorAnalysis.Models.Airline>) ViewBag.AID;
        string sectorddl= "";
        foreach(var sector in sectorlist)
        {
        sectorddl = "<option value='"+sector.Name+"'>"+sector.Name+"</option>";
                <input type="hidden" id="hddsector" value="@sectorddl" />
        }

<script>
    var d = new Date();
    var rowindex = 1;
    function addItem() {
        msg = '<tr><input type="hidden" name="hddrowpindex" value="' + rowindex + '" class="rowcount"/>';
        //added for bringing dropdown
            msg += '<td class="center-fix" >';
            msg += '<select>'; 
            msg += document.getElementById("hddsector").value;
            msg += '</select>'
            msg += '</td>';
        msg += '<td class="nocap-col-sm-2">';
        msg += '<input type="text" class="form-control" style="text-transform:uppercase" name="AgentCode-' + rowindex + '" id="AgentCode-' + rowindex + '" placeholder="Code"/>';
        msg += '</td>';
        msg += '<td class="nocap-col-sm-2">';
        msg += '<input type="text" class="form-control datepicker" name="ArrivalDate-' + rowindex + '" id="ArrivalDate-' + rowindex + '" placeholder="YYYY/MM/DD"/>';
        msg += '</td>';
        msg += '<td class="nocap-col-sm-2">';
        msg += '<input type="text" class="form-control" name="ForPAX-' + rowindex + '" id="ForPAX-' + rowindex + '" placeholder="ForPAX" value="0" />';
        msg += '</td>';
        msg += '<td class="nocap-col-sm-2 center-fix">';
        msg += '<input type="text" class="form-control" name="IndPAX-' + rowindex + '" id="IndPAX-' + rowindex + '" placeholder="IndPAX" value="0" />';
        msg += '</td>';
        msg += '<td class="nocap-col-sm-2 center-fix" style="text-align:center;"><a href="" ><span id="remove-' + rowindex + '"  style="cursor:pointer"><i class="fa fa-trash"></i>Remove</span></a>';
        msg += '&nbsp;<span id="perror-' + rowindex + '" style="display:none"><i class="fa fa-exclamation-triangle faa-exclamation-triangle animated"></i></span></td>';
        msg += '</tr>';
        $('table#portfolio tbody').append(msg);

        rowindex++;
    }
</script>

enter image description here

How do I make the dropdown populated. In this case the first data of Airline table is shown in the dropdown. And how do i get the selected value in the Post method of controller.

enter image description here

  • You need to scrap all this (which will never bind to anything). Refer the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for some options for dynamically adding collection items. –  Jan 23 '18 at 20:30

1 Answers1

0

Your immediate problem is inside of your loop:

var sectorlist=(List<CompetitorAnalysis.Models.Airline>) ViewBag.AID;
string sectorddl= "";
foreach(var sector in sectorlist)
{
    sectorddl = "<option value='"+sector.Name+"'>"+sector.Name+"</option>";
    <input type="hidden" id="hddsector" value="@sectorddl" />
}

It appears to me that you are attempting to build an html string for a select list dropdown. However, on each pass of the loop, you are overwriting the value in sectorddl by using sectorddl = "some value" Perhaps you mean sectorddl += "some value"?

As an aside, this really isn't a good way to build a dropdown in ASP.NET MVC. You can simplify your code by doing something like this:

Controller

var items = from aid in db.Airlines
            select new SelectListItem
            {
                Text = aid.Name, //or whatever the field names are
                Value = aid.Id
            };

ViewBag.AID = items.ToList();

View

@Html.DropDownList(model => model.SelectedAirlineId, 
    new SelectList(ViewBag.AID, "Value", "Text"))
Devin Goble
  • 2,639
  • 4
  • 30
  • 44