我有两个DDL
创建视图
<div class="form-group">
@Html.LabelFor(model => model.Stage, "Stage", new { @class = "control-label col-md-5" })
<div class="col-md-offset-0">
@Html.DropDownList("Stage", String.Empty) @Html.ValidationMessageFor(model => model.Stage)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InternalApprovalAuthorities, "Internal Approval Authority", new { @class = "control-label col-md-8" })
<div class="col-md-offset-1">
@Html.DropDownList("InternalApprovalAuthorities", string.Empty) @Html.ValidationMessageFor(model => model.InternalApprovalAuthorities)
</div>
</div>
&#13;
在我的编辑视图中,我只想查看它们显示的信息。
修改视图
<div class="form-group">
@Html.LabelFor(model => model.Stage, "Stage", new { @class = "control-label col-md-5" })
<div class="col-md-offset-0">
@ViewBag.StageText
@Html.HiddenFor(model => model.Stage)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InternalApprovalAuthorities, "Internal Approval Authority", new { @class = "control-label col-md-5"})
<div class="col-md-offset-0">
@*@Html.Display("InternalApprovalAuthorities")*@
@ViewBag.InternalApprovalAuthoritiesText
@Html.HiddenFor(model => model.InternalApprovalAuthorities)
</div>
</div>
控制器 -
此代码位于我的Get Create
中
ViewBag.InternalApprovalAuthorities = new SelectList(db.InternalApprovalAuthorities, "InternalApprovalAuthoritiesID", "InternalApprovalAuthoritiesText");
&#13;
此代码位于我的用户编辑视图的获取和发布中
Stage stage = db.Stages.Where(t => t.StageID == imsregister.Stage).FirstOrDefault();
ViewBag.StageText = stage.StageText;
InternalApprovalAuthority internalapprovalauthorities = db.InternalApprovalAuthorities.Where(a => a.InternalApprovalAuthoritiesID == imsregister.InternalApprovalAuthorities).FirstOrDefault();
if(internalapprovalauthorities != null)
{
ViewBag.InternalApprovalAuthority = internalapprovalauthorities.InternalApprovalAuthoritiesText;
}
两者都以相同的方式构建。但是Stage返回我需要的文本值。如果我使用@ViewBag.InternalApprovalAuthorities
,则InternalApprovalAuthorities不会返回任何内容,如果我使用Display("InternalApprovalAuthorities")
,则返回int值而不是text。
关于这个问题的任何想法?
答案 0 :(得分:0)
我的问题是我遇到了冲突,因为我的班级名为InternalApprovalAuthority,而我的ViewBag名称相同。为了解决这个问题,我所做的就是在视图和控制器中重命名ViewBag,从而为我提供我想要的文本值。
修改视图
<div class="form-group">
@Html.LabelFor(model => model.InternalApprovalAuthorities, "Internal Approval Authority", new { @class = "control-label col-md-5"})
<div class="col-md-offset-0">
@ViewBag.IAA @Html.HiddenFor(model => model.InternalApprovalAuthorities)
</div>
</div>
控制器GET和POST
InternalApprovalAuthority internalapprovalauthorities = db.InternalApprovalAuthorities.Where(a => a.InternalApprovalAuthoritiesID == imsregister.InternalApprovalAuthorities).FirstOrDefault();
if (internalapprovalauthorities != null)
{
ViewBag.IAA = internalapprovalauthorities.InternalApprovalAuthoritiesText;
}