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

1 回答 1

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并创建一个名为“ EditorTemplateSelectedService.cshtml ”的文件夹,并在那里创建一个与属性 Name( )同名的新视图

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

@model SelectedService
<p>
  <b>@Model.Name</b>   :
  @Html.CheckBoxFor(x => x.IsSelected) <br />
  @Html.HiddenFor(x=>x.Id)
</p>

EditorFor现在在您的主视图中,使用Html Helper 方法调用您的编辑器模板。

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

现在,当您发布表单时,您的模型将拥有ServicesCollection,其中 Selected Checkboxes 将具有属性True值。IsSelected

[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 中读取并创建实体并设置正确的值并保存到数据库。

于 2013-04-12T17:45:56.987 回答