Click here to Skip to main content
14,924,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am expecting auto selection of dropdown Item from model value on page post back, i am using ASP.NET MVC 4, what is the problem in the below code?

Not getting selection on the following columns:
@Html.DropDownListFor(m => m.Trns[i].Executive1Id, (IEnumerable<SelectListItem>)ViewBag.Staff1Id)

@Html.DropDownListFor(m => m.Trns[i].Executive2Id, (IEnumerable<SelectListItem>)ViewBag.Executive2Id)


Model:
------

XML
public class FDCollectionVM
  {
      public List<TermDeposits> FDs { get; set; } // for Prepare UI
      public List<Trn_TermDeposits> Trns { get; set; }

      [Display(Name = "Date")]
      public DateTime TrnDate { get; set; }

      public int TrnTypesId { get; set; }
      public string expectedReceiveableAmount { get; set; }

      [Display(Name = "Cash Tended By Executive")]
      public decimal CashTendedByExecutive { get; set; }

      public bool IsPostBack { get; set; }
  }


View:
-----

<tbody>

                @{int slcount = 1;}
                @for (int i = 0; i < @Model.FDs.Count; i++)
                {
                    <tr id="@i">
                        @Html.HiddenFor(m => m.FDs[i].Id)
                        <td>@slcount</td> @{slcount++;}
                        <td>
                            @Html.HiddenFor(m => m.FDs[i].Client.Name)
                            @Html.HiddenFor(m => m.FDs[i].Client.WorkProfile)
                            @Html.TextBoxFor(m => m.FDs[i].Client.NameWithJobTitle, htmlAttributes: new { @style = "width:200px", Disabled = "True" })
                        </td>
                        <td>
                            @Html.HiddenFor(m => m.FDs[i].Client.AccountNumber)
                            @Html.TextBoxFor(m => m.FDs[i].Client.AccountNumber, new { @style = "width:40px", Readonly = "True", Disabled = "true" })
                        </td>
                        <td>
                            @Html.HiddenFor(m =>m.FDs[i].AccountNumber)
                            @Html.TextBoxFor(m => m.FDs[i].AccountNumber, new { @style = "width:40px", Readonly = "True", Disabled = "true" })
                        </td>
                        <td>
                            @Html.HiddenFor(m => m.FDs[i].RecurringInvestmentAmount)
                            @Html.TextBoxFor(m => m.FDs[i].RecurringInvestmentAmount, new { @style = "width:60px; text-align: right;", Disabled = "true" })
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.FDs[i].ExpectedPaymentAmount, new { @style = "width:60px; text-align: right;" })
                        </td>
                        
                        <td>
                            @Html.DropDownListFor(m => m.Trns[i].CollectedFromId, (IEnumerable<SelectListItem>)@ViewBag.CollectedFromId)
                        </td>
                        <td class="editor-field">
                            @Html.DropDownListFor(m => m.Trns[i].Executive1Id, (IEnumerable<SelectListItem>)ViewBag.Staff1Id)
                                                        
                        </td>

                        <td class="editor-field">
                            @Html.DropDownListFor(m => m.Trns[i].Executive2Id, (IEnumerable<SelectListItem>)ViewBag.Executive2Id)
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.Trns[i].Remarks)
                        </td>
                    </tr>
                }
            </tbody>
        </table>


Controller:
-----------
public ActionResult MakeTrnForFD(FDCollectionVM modelCollection)
        {
            if (ModelState.IsValid)
            {
                var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted });

                try
                {
                    using (scope)
                    {
                        using (var ctx = new _DB())
                        {
                            foreach (var item in modelCollection.Trns)
                            {
                                ctx.Trn_TermDeposits.Add(item);
                            }
                            ctx.SaveChanges();
                        }
                        scope.Complete();

                        return RedirectToAction("Index");
                    }
                }

                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                validationErrors.Entry.Entity.ToString(),
                                validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
                catch (System.InvalidOperationException ex)
                {
                    ViewData["servercreatedError"] = "Sorry! Unable to Save Data: " + ex;
                }

                catch (Exception ex)
                {
                    ViewData["servercreatedError"] = "Sorry! Unable to Save Data: " + ex;
                }
            }
           
            ViewBag.CollectedFromId = new SelectList(db.CollectedFroms, "CollectedFromId", "Name", modelCollection.Trns[0].CollectedFromId);
            ViewBag.Staff1Id = new SelectList(db.ddl_VerifiedBy, "ddl_VerifiedById", "Name");
            ViewBag.Executive2Id = new SelectList(db.ddl_VerifiedBy, "ddl_VerifiedById", "Name");
            modelCollection.IsPostBack = true;
            return View(modelCollection); // HERE I HAVE VERIFIED ALL THE VALUES IS OK.
        }


Many thanks for any help.
Posted
Updated 22-Sep-14 3:37am
v4

1 solution

   
v3
Comments
Raju Mukherjee 23-Sep-14 10:43am
   
I think i have to do it with ViewModel only and not to use the ViewBag for populating DropDownlist, bcz the selected value will be pass through ViewModel only and i am unable to loop through it using the ViewBag.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900