简体   繁体   English

MVC3将模型(某些字段)绑定到视图

[英]MVC3 Binding model (some fields) to the view

Binding model to the view. 将模型绑定到视图。

my model Person has {Id,Title,Address...etc) 我的模特人有{Id,Title,Address ... etc)

In my controller I want to retrieve some fields eg: 在我的控制器中,我想检索一些字段,例如:

 var model = from p in db.Person where p.jobtype == 1 
             select new Person { Title = p.Title, Address = p.Address};
 return View(model);

also tried to to use anonymous type and it doesn't work: 也试图使用匿名类型,它不起作用:

 var model = from p in db.Person where p.jobtype == 1 
             select new { p.Title, p.Address};
 return View(model);

In my view: 在我看来:

 @model IEnumerable<Demo.Models.Person>
 @foreach (var item in Model) {
 <div>@item.Title</div>}  >FAILS

If I retrieve full object then it works, how do I retrieve some fields using anonymous type or\\and using my model.... plz provide the correct syntax. 如果我检索到完整的对象,那么它将起作用,如何使用匿名类型或\\和我的模型来检索某些字段。...plz提供正确的语法。 Thanks 谢谢

If your View is requiring the Model to be Enumerable then it needs to be AsEnumerable in your select statement. 如果你的观点是需要模型是Enumerable那么它需要AsEnumerable在你的SELECT语句。

var model = (from p in db.Person
            where p.jobtype == 1
            select p).AsEnumerable();

return View(model);

and then 接着

@model IEnumerable<Demo.Models.Person>

@foreach (var item in Model.ToList()) {
<div>@item.Title</div>}

Alternatively, Since you just need a list at the end of the day, and you "should" be using a ViewModel, I suggest the following. 或者,由于您只需要一天结束时的列表,并且“应该”使用ViewModel,因此建议以下内容。

ViewModel 视图模型

public class PersonViewModel
{
    public int Name { get; set; }
    public string Title { get; set; }
}

Controller 控制者

var person = (from p in db.Person
            where p.jobtype == 1
            select p);

PersonViewModel model = Mapper.Map<Person, PersonViewModel>(person);

return View(model.ToList());

View 视图

@model List<Demo.ViewModels.PersonViewModel>

@foreach (var item in Model) {
<div>@item.Title</div>}

note: I used AutoMapper in the above example 注意:我在上面的示例中使用了AutoMapper
I don't have my IDE in front of me, so I'm not sure if this is perfect. 我没有我的IDE,所以我不确定这是否完美。

You can use .ToList() : 您可以使用.ToList():

List<person> model = (from p in db.Person 
                      where p.jobtype == 1 
                      select p).ToList();
return View(model);

In your view : 在您看来:

 @model List<person>
 @foreach (var item in Model) {
 }

I would recommend that you call your data db.People or something like that. 我建议您将数据db.People或类似的名称。

You should use p.ID == 3 instead of p.ID = 3 . 您应该使用p.ID == 3而不是p.ID = 3

If your intention is to only select one Person and display then use SingleOrDefault() , First() , FirstOrDefault() to get only a single Person-entry. 如果您打算只选择一个Person并显示,则使用SingleOrDefault()First()FirstOrDefault()仅获得一个Person输入。

var model = (from p in db.Person where p.ID == 3 
             select p.Title).First();

Then in your model don't use IEnumerable, just use: 然后在您的模型中不要使用IEnumerable,只需使用:

 @model Demo.Models.Person
      <div>@item.Title</div>

EDIT: after you updated question to contain p.jobtype instead of p.ID to select multiple I would recommend using a specific ViewModel for the data you wish to select. 编辑:更新问题以包含p.jobtype而不是p.ID选择多个后,我建议对您要选择的数据使用特定的ViewModel。 If all you want is Title then you could use 如果您只想要标题,那么您可以使用

@model IEnumerable<string>
@foreach (var title in Model) {
 <div>@title</div>}

But I would recommend using a specific ViewModel with only the fields you require, for example: 但是我建议仅将特定的ViewModel与所需的字段一起使用,例如:

public class PersonViewModel
{
    public int Name { get; set; }
    public string Title { get; set; }
}

and then select new PersonViewModel {Name = p.Name, Title = p.Title } and of course use that model in the view: @model IEnumerable<PersonViewModel> . 然后select new PersonViewModel {Name = p.Name, Title = p.Title } ,当然@model IEnumerable<PersonViewModel>在视图中使用该模型: @model IEnumerable<PersonViewModel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM