MVC显示适当的值而不是键 [英] MVC displaying appropriate Value instead of Key

查看:47
本文介绍了MVC显示适当的值而不是键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一张桌子:

Person[ID,NAME,GENDER]
id = 1, name = Mozart, gender = 1
id = 2, name = Sissy, gender = 2
...

我有一个 KeyValuePair 集合,

key = 1, value = MALE
key = 2, value = FEMALE
...

如何在MVC视图上显示而不是按性别显示其值?

How can I display on MVC View instead of gender its value ?

like this:
---------
Persons
1 | Mozart | MALE
2 | Sissy | FEMALE
---------

Controller和View尽可能简单:

Controller and the View are as simple as it is possible:

public class HomeController : Controller {
    public List<KeyValuePair<int, string>> genders = new List<KeyValuePair<int, string>>();
    public ActionResult Index() {
        genders.Add(1, "male");
        genders.Add(2, "female");

        return View( dbContext.Persons.ToList() );
    }
}

@model IEnumerable<Models.Person>
...
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.gender)
        </td>
        <td>
            @Html.ActionLink("Edit", "UserEdit", new { id = item.id }) |
            @Html.ActionLink("Delete", "UserDelete", new { id = item.id })
        </td>
    </tr>
}

谢谢

推荐答案

最简单的方法是创建一个包含所需属性的视图模型.

The easiest way to accomplish this would be to create a view model that contains exactly the properties that you need.

查看模型

public class PersonViewModel{
    public int Id {get;set;}

    public string Name {get;set;}

    public string Gender {get;set;}
}

现在,当您填充此视图模型时,可以直接在代码中进行查找并传递适当的信息

Now, when you populate this view model, you can do the lookup directly in your code and pass the appropriate information

var genderDictionary = new Dictionary<int, string>();
genderDictionary.Add(1, "Male");
genderDictionary.Add(2, "Female");
var people = db.Persons.ToList().Select(x=>new PersonViewModel{
     Id = x.Id, 
     Name = x.Name,
     Gender = genderDictionary[x.Gender]
});
return people;

最后,您的视图模型应更新为以下

Lastly, your view's model should be updated to the following

@Model IEnumerable<Namespace.PersonViewModel>

或者,您可以将字典传递给viewdata对象中的视图.然后,在执行循环时使用它并在那里进行查找.但是,在进行.NET MVC开发时,最好使用强类型视图(如上所述).除了应该如何做"以外,这里提供的信息还更多.

Alternatively, you could pass your dictionary to your view in the viewdata object. Then, use that when you are doing your loop and perform the lookup there. However, using strongly-typed views (as above) is the better practice to get into when doing .NET MVC development. This is here more for information than a "how you should do it".

控制器

public ActionResult People(){

   var model = db.Persons.ToList();
   ViewData["genderDictionary"] = yourKeyValuePair;
   return View(model);
}

查看

@{
  var genderDictionary = (Dictionary<int, string>)ViewData["genderDictionary"];
  foreach(var person in Model){
     <tr>
        <td>@person.Name</td>
        <td>@genderDictionary[person.Gender]</td>
     </tr>
  }
}

这篇关于MVC显示适当的值而不是键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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