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

时间:2013-08-22 09:22:49

标签: c# asp.net-mvc templates

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

使用选项3(参见模板代码)它会预先选择国家/地区,但MVC会更改下拉列表的ID和名称,例如 - 如果传递给编辑器模板的名称(属性名称)为“ CountryID “,MVC设置id =”* CountryID_CountryID *“和name =” CountryID.CountryID “。当然,这会在发布值时陷入绑定,因为在View Model属性名称中只是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>

观点: 只显示相关代码

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

VIEWMODEL1: 只显示相关代码

namespace PSP.ViewModels
{
    public class ViewModel1
    {
        public short CountryID { get; set; }
    }
}

国家/地区列表:

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");
        }

CONTROLLER:仅显示相关代码

        public ActionResult Create()
        {

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

            return View(VM1);
        }

1 个答案:

答案 0 :(得分:0)

首先,您不应创建静态方法来获取国家/地区列表。将国家/地区列表添加到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; }
    }
}

然后,在您的Controller Action中,您将拥有:

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);
}

而且,在你的视图中,你将拥有(你不需要EditorTemplate):

@model PSP.ViewModels.ViewModel1

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

    <fieldset>
        <legend>EventList</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CountryID)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.CountryID, 
              new SelectList(Model.Countries, "ID", "NameText", Model.CountryID))  
        </div> 
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<强>更新

如果您想使用EditorTemplate,则必须使用ViewModel中的UIHint属性,如下所示:

namespace PSP.ViewModels
{
    public class ViewModel1 // Choose a more meaningful name for your ViewModel
    {
        [UIHint("CountryID")]
        public short CountryID { get; set; }
        public IEnumerable<Country> CountriesList { get; set; }
    }
}

然后,您创建如下的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 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>
}