带有DropDownList数据的ASP.NET MVC 4 ViewModel [英] ASP.NET MVC 4 ViewModel with DropDownList data

查看:46
本文介绍了带有DropDownList数据的ASP.NET MVC 4 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用@html.EditorFor在编辑模式下渲染我的模型,并且没有显示下拉列表.

I'm using @html.EditorFor to render my model in edit mode, and a dropdownlist is not rendered.

这是我的ViewModel:

Here's my ViewModel:

     public class RiskAutoViewModel
     {
       public RiskAutoViewModel()
       {
         VehicleMakeList = new SelectList(new List<VehicleMake>() { new VehicleMake() { Id = 1, Name = "Renault" }, new VehicleMake() { Id = 2, Name = "Peugeot" } });
       }


    public int NoClaimsDegree { get; set; }

    public int VehicleValue { get; set; }

    public int EngineCapacity { get; set; }

    public int VehicleMake { get; set; }

    public SelectList VehicleMakeList { get; set; }
  }

VehicleMake呈现为文本框,而VehicleMakeList则完全不呈现.我想渲染一个包含VehicleMake列表的下拉列表,并将其值设置为VehicleMake之一.

VehicleMake is rendered as a textbox and VehicleMakeList is not rendered at all. What I'd like is to render a dropdownlist containing the list of VehicleMake and set its value to the one of VehicleMake.

保存模型后,应将VehicleMake设置为列表中所选项目的值.

When the model is saved, VehicleMake should be set to the value of the selected item in the list.

我该怎么做?

编辑

由于我无法在下面的注释框中键入任何代码,因此我将在此处进行后续操作.

Since I can't type any code in the comment boxes below, I'll write a follow up here.

我最终创建了一个EditorTemplate,例如:

I ended up creating a EditorTemplate such as:

<div class="editor-label">
    @Html.LabelFor(model => model.VehicleMakeList)
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.VehicleMake, Model.VehicleMakeList)
    @Html.ValidationMessageFor(model => model.VehicleMake)
</div>

现在我的ViewModel看起来像这样:

And now my ViewModel looks like this:

[Required]
[ScaffoldColumn(false)]
public int VehicleMake { get; set; }

[Display(Name = "Marque", Prompt = "Marque", Description = "Renseigne la marque du véhicule")]
public SelectList VehicleMakeList { get; set; }

现在,这使我想到另一个问题(也许我应该像在其他线程中那样),但实际上该视图中有两个下拉菜单.第二个下拉列表中的项目基本上是动态的,它们取决于第一个下拉列表中选择的项目.使用AJAX很难做到这一点,但是使用MVC,我迷路了.人们通常如何做到这一点?

Now this leads me to another question (maybe I should as in a different thread) but I actually have TWO dropdowns in that View. And the items in the second dropdown are basically dynamic and they depend on the item selected in the first dropdown. This is dead easy to do with AJAX but with MVC I'm lost. How do people do that usually ?

推荐答案

不幸的是,模板编辑器中没有内置的下拉列表支持.您可以编写自己的编辑器模板,也可以在视图中使用html帮助器方法@Html.DropDownListFor().

Unfortunately, there is no built in support for drop down lists in the editor for templates. You can either write your own editor template, or use the html helper method @Html.DropDownListFor() in your view.

达林·迪米特洛夫(Darin Dimitrov)对此问题的答案如果您愿意,可以引导您完成为下拉列表构建编辑器模板的过程.

Darin Dimitrov's answer to this question can walk you through the process of building an editor template for drop down lists, if you are so inclined.

最快实现此目的的方法是在您看来:

The quickest way to get this working would be to do this in your view:

@Html.EditorFor(model => model.NoClaimsDegree)
@Html.EditorFor( model => model.VehicleValue )
@Html.EditorFor( model => model.EngineCapacity )
@Html.DropDownListFor( model => model.VehicleMake, Model.VehicleMakeList, "Select a make" )

这篇关于带有DropDownList数据的ASP.NET MVC 4 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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