动态生成的下拉列表使用mvc 4选择中的强类型视图助手消失在页面回发上 [英] Dynamically generated drop down list using strongly type view helper in mvc 4 selection disappear on page postback

查看:60
本文介绍了动态生成的下拉列表使用mvc 4选择中的强类型视图助手消失在页面回发上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待从页面上的模型值自动选择下拉列表项目,我使用的是ASP.NET MVC 4,下面的代码有什么问题?



未在以下列中进行选择:

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



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

< br $>


型号:

------



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 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.

推荐答案

看看这个



http:// techbrij.com/create-dynamic-dropdownlists-in-asp-net [ ^ ]


这篇关于动态生成的下拉列表使用mvc 4选择中的强类型视图助手消失在页面回发上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆