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 を作成しましょう。「 EditorTemplateViews/YourControllerName 」というフォルダーに移動して作成し、そこにプロパティ Name( )と同じ名前の新しいビューを作成します。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持つコレクションがモデルに作成されます。TrueIsSelected

[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);
}

ビューモデルから読み取ってエンティティを作成し、適切な値を設定してデータベースに保存できます。

于 2013-04-12T17:45:56.987 に答える