从来自另一个控制器的列表中为下拉列表设置默认值 [英] Set a default value for a dropdownlist from a list coming from another controller

查看:202
本文介绍了从来自另一个控制器的列表中为下拉列表设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网页显示一个对象的属性,以便我可以编辑它,并且使用来自另一个类的字符串填充一个 DropDownList



这是我用来填充 DropDownList 的方法:

  private void PopulateOBJSetDropdownList(object selectedobj = null)
{
列表< string> listBJSetName = m_OBJSetManager.GetListOBJSets()。OrderBy(x => x.m_Name)
.Select(x => x.m_Name.ToString())
.Distinct()
。 ToList();
ViewBag.objSetID = new SelectList(listOBJSetName);
}

ViewBag 属性做得很好,但编辑项目时清单是空的。



我很确定这是因为这一行:

 < div class =editor-label> 
@ Html.LabelFor(model => model.m_OBJSetID,Obj Set)
< / div>
< div class =editor-field>
@ Html.DropDownList(objSetID,String.Empty)
@ Html.ValidationMessageFor(model => model.m_OBJSetID)
< / div>

因为下拉列表填充了 String.Empty 。这是来自objs的控制器。



基本上,我想要这个 DropDownList 向我显示所有的名称objSets可用,但我也希望它在编辑对象时默认选择正确的objSet。



有人可以帮助吗?我清楚了吗谢谢大家。

解决方案

我会避免viewbag。您可能需要创建一个视图模型,并通过它。
,但这也可以用viewbag来完成。



首先,在你看来,我会将你的下拉菜单更改为以下

  @ Html.DropDownListFor(model => model.m_OBJSetID,DDLSelectitemListGoesHere)

如果您执行视图模型,则可以包含本页需要在一个类中使用的所有内容,并将其发送到视图

  public class MyViewModel {
public List< YourModel>模特{get;组; }
public IEnumerable< SelectListItem> DDLItems {get;组; }
}

然后在您的视图中,顶部

  @model PROJECTNAME.NAMESPACE.MyViewModel 

,你可以这样填写下拉列表

  @ Html.DropDownListFor(model => model.theModel。 m_OBJSetID,model.DDLItems)

希望其中一个可以让你通过


I have this web page which shows the property of an object so that I may edit it, and I populate a DropDownListwith strings coming from another class.

Here's the method I use to populate the DropDownList:

private void PopulateOBJSetDropdownList(object selectedobj = null)
        {
            List<string> listOBJSetName = m_OBJSetManager.GetListOBJSets().OrderBy(x => x.m_Name)
                                                           .Select(x => x.m_Name.ToString())
                                                           .Distinct()
                                                           .ToList();
            ViewBag.objSetID = new SelectList(listOBJSetName );
        }

The ViewBagproperty does its job quite well, but the list comes empty when editing the item.

I'm pretty sure it is because of this line:

<div class="editor-label">
            @Html.LabelFor(model => model.m_OBJSetID, "Obj Set")
        </div>
        <div class="editor-field">
            @Html.DropDownList("objSetID ", String.Empty)
            @Html.ValidationMessageFor(model => model.m_OBJSetID)
        </div>

Because the dropdownlist is populated with String.Empty. This comes from a controller of objs.

Basically, I want this DropDownList to show me all the names of the objSets available, but I would also want it to have the correct objSet selected by default when editing an obj.

Does anyone can help? Am I clear enough? Thank you everyone.

解决方案

i would avoid the viewbag. you might want to create a view model, and pass that instead. but this can be done with the viewbag as well.

first, on your view, i would change your dropdown to the following

@Html.DropDownListFor(model => model.m_OBJSetID, DDLSelectitemListGoesHere)

if you do a view model, you can contain everything this page needs to use in one class, and send it to the view

public class MyViewModel{
    public List<YourModel> theModel { get; set; }
    public IEnumerable<SelectListItem> DDLItems { get; set; }
}

then on your view, at the top

@model PROJECTNAME.NAMESPACE.MyViewModel

and you can fill in the drop down like so

@Html.DropDownListFor(model => model.theModel.m_OBJSetID, model.DDLItems)

hopefully one of those will get you through

这篇关于从来自另一个控制器的列表中为下拉列表设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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