Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Asp.net mvc 3 将不同模型的查询传递给不同模型的视图_Asp.net Mvc 3_Entity Framework_Razor - Fatal编程技术网

Asp.net mvc 3 将不同模型的查询传递给不同模型的视图

Asp.net mvc 3 将不同模型的查询传递给不同模型的视图,asp.net-mvc-3,entity-framework,razor,Asp.net Mvc 3,Entity Framework,Razor,我有一个位置模型和一个服务模型,使用应用程序的上下文类,我可以在位置的创建方法中查询服务模型,但是如何在绑定到位置的强类型视图中访问这些结果 namespace LocationApp.Models { public class Location { public Location() { this.ServiceAssignments = new HashSet<ServiceAssignment>();

我有一个
位置
模型和一个
服务
模型,使用应用程序的上下文类,我可以在位置的创建方法中查询服务模型,但是如何在绑定到位置的强类型视图中访问这些结果

namespace LocationApp.Models
{
    public class Location
    {
        public Location()
        {
            this.ServiceAssignments = new HashSet<ServiceAssignment>();
        }

        public int id { get; set; }
        public string name { get; set; }
        public bool active { get; set; }

        public virtual ICollection<ServiceAssignment> ServiceAssignments { get; set;  }
    }
}

namespace LocationApp.Models
{
    public class Service
    {
        public Service()
        {
            this.ServiceAssignments = new HashSet<ServiceAssignment>();
        }

        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public bool active { get; set; }
        public string icon { get; set; }

        public virtual ICollection<ServiceAssignment> ServiceAssignments { get; set; }
    }
}

public ActionResult Create()
{
    using (var db = new LocationAppContext())
    {
        var serv = (from s in db.Services
                    where s.active == true
                    select s).ToList();

         if (serv.Count > 0)
         {
             return View(serv);
         }
         else
         {
             return View();
         }
    }
} 
namespace LocationApp.Models
{
公共类位置
{
公共场所()
{
this.ServiceAssignments=new HashSet();
}
公共int id{get;set;}
公共字符串名称{get;set;}
公共bool活动{get;set;}
公共虚拟ICollection服务分配{get;set;}
}
}
名称空间位置app.Models
{
公务舱服务
{
公共服务()
{
this.ServiceAssignments=new HashSet();
}
公共int id{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共bool活动{get;set;}
公共字符串图标{get;set;}
公共虚拟ICollection服务分配{get;set;}
}
}
公共操作结果创建()
{
使用(var db=new LocationAppContext())
{
var serv=(从数据库服务中的s)
其中s.active==true
选择s.ToList();
如果(服务计数>0)
{
返回视图(serv);
}
其他的
{
返回视图();
}
}
} 

您需要创建一个新的ViewModel

public class CreateLocationVM
{
  public string Name { set; get;}
  public List<SelectedService> Services { set; get;}  
  public CreateLocationVM()
  {
     Services=new List<SelectedService>();
  }
}
public class SelectedService
{
  public int ID { set; get;}
  public string Name {set; get;}
  public bool IsSelected { set; get;}
}
现在让我们创建一个EditorTemplate。转到
Views/YourControllerName
,创建一个名为“EditorTemplate”的文件夹,并在那里创建一个与属性名同名的新视图(
SelectedService.cshtml

将此代码添加到新的编辑器模板中

@model SelectedService
<p>
  <b>@Model.Name</b>   :
  @Html.CheckBoxFor(x => x.IsSelected) <br />
  @Html.HiddenFor(x=>x.Id)
</p>
现在,当您发布表单时,您的模型将具有
服务
集合,其中所选复选框将具有
True
属性的

[HttpPost]
public ActionResult Create(CreateLocationVM model)
{
   if(ModelState.IsValid)
   {
      //Check for model.Services collection and Each items
      //  IsSelected property value.
      //Save and Redirect(PRG pattern)
   }
   return View(model);
}

您可以从Viewmodel中读取数据,创建实体,设置正确的值并保存到数据库。

视图中显示了什么?要显示服务列表还是位置列表?创建视图显示用于创建新位置的表单,我要为每个服务添加复选框列表。因此,您可以将特定的服务绑定到一个位置
@model CreateLocationVM
<h2>Add Location</h2>
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>    
    <div>  
      @Html.EditorFor(m=>m.Services)         
    </div>    
    <input type="submit" value="Submit" />
}
[HttpPost]
public ActionResult Create(CreateLocationVM model)
{
   if(ModelState.IsValid)
   {
      //Check for model.Services collection and Each items
      //  IsSelected property value.
      //Save and Redirect(PRG pattern)
   }
   return View(model);
}