MVC列表强类型查看DropDownListFor不显示当前值 [英] Mvc List Strongly type View DropDownListFor not showing current value
问题描述
我有以下 dropdownlistfor
控制
@Html.DropDownListFor(m => item.RelationShipId, new SelectList(ViewBag.RelationShip,"Id", "Title"), new { @id = "ddl_" + @item.UserID, @class = "form-control", onchange = "update(this,'" + @item.UserID + "','" + @item.SentByUserId + "');" })
我这个绑定强类型视图这样的 @model名单< MvcUI.Models.UserDetails>
在 ViewBag.RelationShip
我下面的列表
public static List<IDTitleDTO> DataTableToRelationshipList(this DataTable table)
{
var list = table.AsEnumerable()
.Select(dr =>
new IDTitleDTO
{
Id = dr.Field<int>("RelationShipId"),
Title = dr.Field<string>("RelationShipName")
}).ToList();
return list;
}
我传递的List 的UserDetails
模型对我的看法。这里是我的的UserDetails
模式
I am passing List Of UserDetails
model to my view. Here is my UserDetails
Model
public class UserFriendDetails
{
public string UserID { get; set; }
public string UserName { get; set; }
public string Firstname { get; set; }
public int? RelationShipId { get; set; }
....
...
Other fields
}
在列表假设我有3个纪录。从他们的2条RelationShipId为空。如果是null,则我设置-1是 - 选择 - 在剩下的一个记录我有RelationShipId想我有13.视图中打开它绑定 - 选择 - 对于前两个记录。但对于第三个记录是没有约束力的ViewBag适当的值。哪里是问题,我无法弄清楚。有些人可以帮我解决这个?
Suppose in a list I have 3 record. Out of them 2 records RelationShipId is null. If it's null then I am setting -1 which is -- Select --. In remaining one record I have RelationShipId suppose I have 13. When view opens It is binding -- Select -- for first two record. But for 3rd record it is not binding appropriate value from ViewBag. I can't figure it out where is the issue. Can some one help me to solve this?
在这里,我创建了一个简单的动作即用户
Here I have created one simple action i.e. user
[AllowAnonymous]
public ActionResult User()
{
var model = new List<UserDetails>();
model.Add(new UserDetails { Id = 1, Fname = "ajay", RelationId = 3});
model.Add(new UserDetails { Id = 2, Fname = "vijay", RelationId = 1 });
model.Add(new UserDetails { Id = 3, Fname = "John", RelationId = 2 });
var rList = new List<IdTitleDTO>();
rList.Add(new IdTitleDTO { Id = 1 , Title = "M"});
rList.Add(new IdTitleDTO { Id = 2, Title = "F" });
rList.Add(new IdTitleDTO { Id = 3, Title = "B" });
ViewBag.Rel = rList;
return View(model);
}
下面是我的 user.cshtml
@model List<WebApplicationDropDown.Models.UserDetails>
<table class="table">
<tr>
<th>
Fname
</th>
<th></th>
</tr>
@*@foreach (var item in Model) {*@
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => Model[i].Fname)
</td>
<td>
@Html.DropDownListFor(modelItem => Model[i].RelationId, new SelectList(ViewBag.Rel, "Id", "Title"), "--Select--")
</td>
</tr>
}
</table>
在此查看它没有约束力relationId。默认情况下它显示 - 选择 -
,而不是呈现如下关系
In this View it is not binding relationId. By default it is showing -- Select --
instead of showing following relation
ajay - B
Vijay - M
John - F
请参阅下图
编辑模板的UserDetails。 CSHTML
Editor template UserDetails.cshtml
@model WebApplicationDropDown.Models.UserDetails
@Html.DropDownListFor(m => m.RelationId, (SelectList)ViewData["RelationShipList"])
View.cshtml
@model IEnumerable<WebApplicationDropDown.Models.UserDetails>
<table class="table">
<tr>
<th>
Fname
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Fname)
</td>
<td>
@*@Html.DropDownListFor(modelItem => item.RelationId, new SelectList(ViewBag.Rel, "Id", "Title"), "--Select--")*@
@Html.EditorFor(m => m, new { RelationShipList = new SelectList(ViewBag.Rel, "Id", "Title") })
</td>
</tr>
}
</table>
请参见下面的截图
推荐答案
创建自定义的 EditorTemplate
类型的 UserFriendDetails
在 /Views/Shared/EditorTemplates/UserFriendDetails.cshtml
@model UserFriendDetails
@Html.TextBoxFor(m => m.UserName)
....
@Html.DropDownLstFor(m => m.RelationShipId, (SelectList)ViewData["RelationShipList"])
然后在主视图,通过的SelectList
使用附加视图数据模板
@model IEnumerable<UserFriendDetails>
....
@Html.EditorFor(m => m, new { RelationShipList = new SelectList(ViewBag.RelationShip,"Id", "Title") })
这将产生正确的名称属性绑定到你的收集,例如:
This will generate the correct name attributes to bind to you collection, for example
<select name="[0].RelationShipId" ..>
<select name="[1].RelationShipId" ..>
和选择的基础上每 RelationShipId
属性的值正确的选项。
and select the correct option based on the value of each RelationShipId
property.
附注:我建议你删除新{@id =ddl_+ @ item.UserID,...
并使用默认的 ID
由助手生成的。它不清楚是什么的onchange
属性做什么,但我也建议你使用的非侵入式JavaScript 而不是行为污染您的标记。
Side note: I suggest you remove the new { @id = "ddl_" + @item.UserID,...
and use the default id
generated by the helper. Its not clear what the onchange
attribute is doing but I also suggest you use unobtrusive javascript rather than polluting your markup with behavior.
这篇关于MVC列表强类型查看DropDownListFor不显示当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!