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:
------
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 = 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);
}
Many thanks for any help.