4

index.cshtmlビューがレコードをループしてそれらを表示するのと同じ方法で(以下のように)、複数のレコードを編集するためのビューを持つことは可能ですか?

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.tvid)
    </td>

したがって、上記の各行について、データベース内の異なる行に関連します。

これがどのように達成されるかを示す例を誰かが知っていますか?

ポインタをありがとう、

マーク

アップデート

モデル:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

namespace MvcObjectives.Models
{
public class objectives
{
    public int ID { get; set; }
    public int tvid { get; set; }
    public string tlnt { get; set; }
    public DateTime month { get; set; }

    public string objective  { get; set; }
    public int score { get; set; }
    public int possscore { get; set; }
    public string comments { get; set; }
 }

}

コントローラ:

 [HttpPost]
    public ActionResult Edit(objectives objectives)
    {
        if (ModelState.IsValid)
        {
            db.Entry(objectives).State = EntityState.Modified;
            foreach (objective Objective in objectives.objective)

            { }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(objectives);
    }

誰かが何か援助を提供できるなら、私はコントローラーに固執していますか?

再度、感謝します、

マーク

2回目の更新

レコードをビューに送信するGETコントローラーは次のとおりです。

        // GET: /Objective/Edit/
        public ActionResult Edit()
        {
            return View(db.objectives.ToList());
        }

POSTコントローラー(値がビューからポストバックされる場所)は次のとおりです。

        // POST: /Objective/Edit/
        [HttpPost]
        public ActionResult Edit(List<objectives> objectives)
        {
            if (ModelState.IsValid)
            {
         // the next part is where I am stuck - how to loop through the returned objectives, and update the records in the database
                db.Entry(objectives).State = EntityState.Modified;
                foreach (objectives obj in objectives)
                {
                    var tempObj = (from objv in db.objectives
                                   where objv.ID==obj.ID
                                   select objv).First();
              }

              //  to do - how to save the updates sent back????

                return RedirectToAction("Index");
            }
            return View(objectives);
        }
4

3 に答える 3

7

エディタテンプレートを使用する

ViewModel/Modelが次のようになっていると仮定します

public class UserViewModel 
{
  public int UserId { set;get;}
  public string Name { set;get;}   
  public IEnumerable<Address> Addresses { set;get;}

  public UserViewModel()
  {
     if(this.Addresses==null)
         this.Addresses=new List<Address>();
  }
}
public class Address
{
  public int AddressID { set;get;}
  public string AddressLine1 { set;get;}
}

addresses.cshtml次に、以下のコンテンツで呼び出されるエディターテンプレートを作成します。

@model YourNameSpace.Address
@Html.TextBoxFor(x => x.AddressLine1)

メインビューでは、これを次のように呼び出すことができます

@model UserViewModel 
@using (Html.BeginForm())
{
  //other elements
 @Html.EditorFor(m=>m.Addresses)
 <input type="submit" value="Save" />
}

これで、HttpPostAtionメソッドでデータを取得できます

[HttpPost]
public ActionResult Save(UserViewModel model)
{  
   foreach (Address address in model.Addresses)
   {
      //now check for address.AddressLine here
   } 
}

編集 :ユーザーのコメントと質問の更新に基づいています。

OPへのリクエスト次回質問を投稿するときは、最初に質問に関連するすべての詳細を含めてください。)

ViewModelを作成して、ListofObjectiveクラスをラップします。

public class ObjectivesEdit
{
    public IEnumerable<Objective> Objectives { set; get; }
    public ObjectivesEdit()
    {
        if (Objectives == null)
            Objectives = new List<Objective>();
    }
}

そして、GETアクションメソッドで、このラッパービューモデルを値が入力されたビューに送信します

  public ActionResult Edit()
  {
     ObjectivesEdit objEdit = new ObjectivesEdit();
     List<Objective> objList = new List<Objective>();
       // you can replace this manual filling with data from database
     objList.Add(new Objective { ID = 1, score = 65 });
     objList.Add(new Objective { ID = 2, score = 43 });
     objList.Add(new Objective { ID = 3, score = 78 });
     objEdit.Objectives = objList;
     return View(objEdit);
  }

Editorテンプレートは次のようになります。名前を付ける必要がありますobjective.cshtml

@model EditorTemplateDemo.Models.Objective           
<p>
Score for @Model.ID is  @Html.TextBoxFor(x => x.score)
@Html.HiddenFor(x => x.ID)
</p>

そしてあなたのメインビュー

@model EditorTemplateDemo.Models.ObjectivesEdit
@using (Html.BeginForm())
{
  @Html.EditorFor(x=>x.Objectives)
  <input type="submit" value="Save" />    
}

そして最後に、HTTPPOSTアクションメソッドは次のようになります

    [HttpPost]
    public ActionResult Edit(ObjectivesEdit model)
    {
        if (model.Objectives != null)
        {
            // Put a break point here and you will see the posted data
            foreach (var item in model.Objectives)
            {
                 context.Entry(item).State = EntityState.Modified;
            }
            //Save and redirect  
            context.SaveChanges();
            return RedirectToAction("Index");     
        }
        return View(model);
    }

これは機能するはずです。テスト済み。

これ以上の質問を避けるために、ここで上記のコードの作業サンプルを共有しています。コード内でVisualStudioブレークポイントを使用して、投稿されている値を確認してください。

于 2012-05-29T13:53:36.403 に答える
1

Person ..というモデルがあり、ビュー内の多数の人物を編集してアクションに投稿するとします。

public ViewResult Edit()
{
    return View(list of persons to edit);
}

public ViewResult Edit(List<Person> persons)
{
    // save to db?
}

次に、編集する複数の人を表示するビューを作成します。

Edit.cshtml

@model List<Person>

@for (int i = 0; i < Model.Count; i++) {
   <h4>Person Number: @i</h4>
   @:First Name: @Html.EditorFor(m => m[i].FirstName)
   @:Last Name: @Html.EditorFor(m => m[i].LastName)
}
于 2012-05-29T13:57:11.007 に答える
0

EditorTemplatesを見て ください

于 2012-05-29T13:52:28.327 に答える