2013-04-12 55 views
0

我有一个Location模型和Services模型,使用应用程序的上下文类我可以在位置的create方法中对服务模型执行查询,但是如何在强制类型化视图的视图中访问这些结果?将不同模型的查询传递给不同模型的视图

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(); 
     } 
    } 
} 
+0

你在视图中显示了什么?你想显示一个服务列表或位置列表? – Shyju

+0

创建视图显示用于创建新位置的表单,我想为每个服务添加复选框列表。因此,您可以将特定服务绑定到某个位置。 – TheWebs

+0

查看我使用编辑器模板的答案 – Shyju

回答

0

您需要创建一个新的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;} 
} 

现在,在您GET行动,创建视图模型的对象,并填写Services集合属性,并将其发送给视图。

public ActionResult Create() 
{ 
    var vm = new CreateLocationVM(); 

    //The below code is hardcoded for demo. you mat replace with DB data. 
    vm.Services.Add(new SelectedService{ Name = "Test1" , Id=1}); 
    vm.Services.Add(new SelectedService{ Name = "Test2", Id=2 }); 

    return View(vm); 
} 

现在来创建一个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> 

现在在主视图中,使用EditorFor HTML辅助方法调用你的编辑模板。

@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" /> 
} 

现在,当您发布的形式,你的模型将有Services收集所选择的复选框将具有用于IsSelected物业一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); 
} 

您可以从您的视图模型读取和创建您的实体,并设置适当的值并保存到数据库中。

相关问题