Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义编辑器模板时未选择下拉列表值_C#_Asp.net Mvc_Templates - Fatal编程技术网

C# 使用自定义编辑器模板时未选择下拉列表值

C# 使用自定义编辑器模板时未选择下拉列表值,c#,asp.net-mvc,templates,C#,Asp.net Mvc,Templates,我有一个用于下拉列表的简单自定义编辑器模板,其中显示了国家列表(国家名称、数字ID)。我通过ViewModel1将数字ID传递给视图,以便在下拉列表中已经选择了特定的国家/地区。即使模型包含CountryID,也不会选择国家id 使用选项3(参见模板代码),它确实预先选择了国家,但MVC更改了下拉列表的id和名称,例如-如果传递给编辑器模板的名称(属性名称)是“CountryID”,MVC会设置id=“*CountryID\u CountryID*”和name=“CountryID.Countr

我有一个用于下拉列表的简单自定义编辑器模板,其中显示了国家列表(国家名称、数字ID)。我通过ViewModel1将数字ID传递给视图,以便在下拉列表中已经选择了特定的国家/地区。即使模型包含CountryID,也不会选择国家id

使用选项3(参见模板代码),它确实预先选择了国家,但MVC更改了下拉列表的id和名称,例如-如果传递给编辑器模板的名称(属性名称)是“CountryID”,MVC会设置id=“*CountryID\u CountryID*”和name=“CountryID.CountryID”。当然,如果在视图模型属性名为just CountryID的情况下发布值,那么这会打乱绑定

问题:我需要在自定义编辑器模板代码中执行什么操作,以便在国家/地区列表下拉列表中预先选择国家/地区?传递给视图的模型包含国家的CountryID

编辑器模板代码:

@model short

@using PSP.Lib;

@{
    SelectList TheSelectList = null;
    string FieldName = ViewData.ModelMetadata.PropertyName;  //FieldName only used when I tried the commented out option.

    TheSelectList = DLists.GetCountriesList();  //just gets list of countries and a numeric id for each.
}


        <div class="editor-label">
            @Html.LabelFor(model => model)
        </div>
        <div class="editor-field">
            @Html.DropDownList("", TheSelectList)  //==1. country id passed thru model does not get selected on list.
         @* @Html.DropDownListFor(model => model, TheSelectList)  *@   //==2. as above, does not work.
         @* @Html.DropDownList(FieldName, TheSelectList) *@  //==3. country id passed thru model DOES get selected BUT, id and name parameters get changed.
        </div>
public static SelectList GetCountriesList()
        {
            AccDBEntities db = new AccDBEntities();
            var ls = (from ct in db.Countries
                      select new { Text = ct.NameText, Value = ct.ID, Selected = false }).ToList();
            return new SelectList(ls, "Value", "Text");
        }
国家/地区列表:

@model short

@using PSP.Lib;

@{
    SelectList TheSelectList = null;
    string FieldName = ViewData.ModelMetadata.PropertyName;  //FieldName only used when I tried the commented out option.

    TheSelectList = DLists.GetCountriesList();  //just gets list of countries and a numeric id for each.
}


        <div class="editor-label">
            @Html.LabelFor(model => model)
        </div>
        <div class="editor-field">
            @Html.DropDownList("", TheSelectList)  //==1. country id passed thru model does not get selected on list.
         @* @Html.DropDownListFor(model => model, TheSelectList)  *@   //==2. as above, does not work.
         @* @Html.DropDownList(FieldName, TheSelectList) *@  //==3. country id passed thru model DOES get selected BUT, id and name parameters get changed.
        </div>
public static SelectList GetCountriesList()
        {
            AccDBEntities db = new AccDBEntities();
            var ls = (from ct in db.Countries
                      select new { Text = ct.NameText, Value = ct.ID, Selected = false }).ToList();
            return new SelectList(ls, "Value", "Text");
        }
控制器:仅显示相关代码

@model PSP.ViewModels.ViewModel1

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EventList</legend>

            @Html.EditorFor(model => model.CountryID)
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
namespace PSP.ViewModels
{
    public class ViewModel1
    {
        public short CountryID { get; set; }
    }
}
        public ActionResult Create()
        {

            ViewModel1 VM1 = new ViewModel1();
            VM1.CountryID = 50;    //just pre-selecting a country id in the dropdown list

            return View(VM1);
        }

首先,您不应该创建一个静态方法来获取国家列表。将国家/地区列表添加到ViewModel,如下所示:

namespace PSP.ViewModels
{
    public class ViewModel1 // Choose a more meaningful name for your ViewModel
    {
        public short CountryID { get; set; }
        public IEnumerable<Country> CountriesList { get; set; }
    }
}
而且,在您看来,您将拥有(您不需要EditorTemplate):

然后,创建一个如下所示的PartialView,将其命名为
CountryID.cshtml
,并将其置于
Views\Shared\EditorTemplates

@model short

<div class="editor-label">
    @Html.LabelFor(model => model)
</div>
<div class="editor-field">
    @Html.DropDownList("", 
      new SelectList(ViewBag.Countries, "ID", "NameText", Model)
</div>
@model short
@LabelFor(model=>model)
@Html.DropDownList(“,
新的选择列表(ViewBag.Countries,“ID”,“NameText”,型号)
在主视图中,您将拥有:

public ActionResult Create()
{

    ViewModel1 VM1 = new ViewModel1();
    VM1.CountryID = 50;    //just pre-selecting a country id in the dropdown list
    using(AccDBEntities db = new AccDBEntities())
    {
        VM1.CountriesList = (from ct in db.Countries
                             select ct).ToList(); 
    }

    return View(VM1);
}
@model PSP.ViewModels.ViewModel1

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EventList</legend>

        @Html.EditorFor(model => model.CountryID, Model.Countries)

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
@model PSP.ViewModels.ViewModel1
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
事件列表
@EditorFor(model=>model.CountryID,model.Countries)

}
你能发布
DLists.GetCountriesList()的代码吗
和您的控制器操作?根据要求,我已发布了代码,谢谢。我想您完全错过了我的问题。上面的名称仅供参考,我的代码中有更好的名称,这不是我的问题。出于各种原因,我确实希望使用编辑器模板。我知道如何在没有它的情况下使其工作。您能告诉我如何创建编辑器模板吗编辑器模板工作吗?(这是我的问题)。